// 实现文件:声明在对应的.h文件。继承至UIViewControllor
- (void)viewDidLoad
{
[super viewDidLoad];
// 按钮
self.stp = [[UIStepper alloc] initWithFrame:CGRectMake(20, 30, 150, 30)];
self.stp.tintColor = [UIColor whiteColor];
self.stp.value = 5;
self.stp.maximumValue = 5;
[self.view addSubview:self.stp];
// 默认背景
self.view.backgroundColor = [[UIColor alloc]initWithPatternImage:
[UIImage imageNamed:@"beijing1.jpg"]];
// 创建视图
self.imv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
// 自动缩放
self.imv.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.imv];
// 事件
[self.stp addTarget:self action:@selector(changeState) forControlEvents:UIControlEventValueChanged];
}
- (void)changeState
{
// 每次都重置,保证是从6plus的基础上改变
self.imv.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
// default is iPhone6plus: 414 735
switch ((int)self.stp.value)
{
case Phone:// 240 320
self.imv.frame = CGRectInset(self.imv.frame, 174, 215);
self.imv.image = [UIImage imageNamed:@"Phone.jpg"];
break;
case iPhone4:// 240 320
self.imv.frame = CGRectInset(self.imv.frame, 174, 216);
self.imv.image = [UIImage imageNamed:@"iPhone5.jpg"];
break;
case iPhone5:// 320 568
self.imv.frame = CGRectInset(self.imv.frame, 94, 168);
self.imv.image = [UIImage imageNamed:@"iPhone5.jpg"];
break;
case iPhone6:// 375 667
self.imv.frame = CGRectInset(self.imv.frame, 39, 69);
self.imv.image = [UIImage imageNamed:@"iPhone6.jpg"];
break;
case iPhone6plus:// 414 736
self.imv.frame = CGRectInset(self.imv.frame, 0, 0);
self.imv.image = [UIImage imageNamed:@"iPhone6plus.jpg"];
break;
default:
self.view.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"beijing.jpg"]];
break;
}
NSLog(@"%f", self.stp.value);
}