ios實現button變換顏色並可以放大、縮小、旋轉,iosbutton
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];
_window.backgroundColor = [UIColorwhiteColor];
[_windowmakeKeyAndVisible];
//定義一個父視圖
UIView *superView = [[UIViewalloc] initWithFrame:CGRectMake(65,65, 300, 300)];
[_window addSubview:superView];
_views = [[NSMutableArrayalloc] init];
//迴圈建立七個子視圖
for (int i =0; i < 7; i ++) {
UIView *view = [[UIViewalloc] initWithFrame:CGRectMake(50,50, 250, 250)];
//設定隨機色
view.backgroundColor = [UIColorcolorWithRed:arc4random()%10*.1green:arc4random()%10*.1blue:arc4random()%10*.1alpha:1];
[_window addSubview:view];
UIView *lastView = [_viewslastObject];
if (lastView != nil) {
view.transform = CGAffineTransformScale(lastView.transform,0.8, 0.8);
}
[_views addObject:view];
}
//在介面上添加幾個button,來實現不同的功能
UIButton *button1 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:@"旋轉"forState:UIControlStateNormal];
button1.tag = 101;
button1.frame = CGRectMake(50, 320, 50, 30);
[button1 addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[_window addSubview:button1];
UIButton *button2 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:@"放大"forState:UIControlStateNormal];
button2.tag = 102;
button2.frame = CGRectMake(150, 320, 50, 30);
[button2 addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[_window addSubview:button2];
UIButton *button3 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[button3 setTitle:@"縮小"forState:UIControlStateNormal];
button3.tag = 103;
button3.frame = CGRectMake(250, 320, 50, 30);
[button3 addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[_window addSubview:button3];
_index = 0;
return YES;
}
//button的點擊事件
- (void)buttonAction:(UIButton *)button{
if (button.tag ==103) {
for (UIView *viewin _views) {
view.transform = CGAffineTransformScale(view.transform, .5, .5);
}
}else if (button.tag ==102)
{
for (UIView *viewin _views) {
view.transform = CGAffineTransformScale(view.transform, 2, 2);
}
}else{
//開啟一個定時器
[NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(timeAction:)userInfo:nilrepeats:YES];
}
}
//定時器
- (void)timeAction:(NSTimer *)time{
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:1.9];
for (UIView *viewin _views) {
view.backgroundColor = [UIColorcolorWithRed:arc4random()%10*.1green:arc4random()%10*.1blue:arc4random()%10*.1alpha:1];
view.transform = CGAffineTransformRotate(view.transform,M_PI/10);
}
[UIViewcommitAnimations];
_index++;
if (_index ==10) {
for (UIView *viewin _views) {
[view removeFromSuperview];
}
[time invalidate];
}
}
模擬器開啟的介面旋轉放大縮小