放大、缩小、旋转、开始动画

来源:互联网 发布:股票数据抓取软件 编辑:程序博客网 时间:2024/05/16 11:23

AppDelegate.m

#import "AppDelegate.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];        _index = 0;        //创建可变数组    _viewArrary = [[NSMutableArray alloc] initWithCapacity:7];        for (int i=0; i<7; i++)    {        //创建视图,并且设置frame值        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 220, 220)];      <span style="color:#ff0000;">  //设置背景颜色,使用三色值(0,1)</span>        view.backgroundColor = [UIColor colorWithRed:arc4random()%10*.1 green:arc4random()%10*.1 blue:arc4random()%10*.1 alpha:1];        //添加到window上显示        [self.window addSubview:view];                //取得前一个视图        UIView *lastView = [_viewArrary lastObject];                if (lastView != nil)        {            view.transform = CGAffineTransformScale(lastView.transform, .7, .7);        }                [_viewArrary addObject:view];    }        //添加一个旋转按钮    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    button1.frame = CGRectMake(50, 400, 50, 20);    button1.tag = 101;    button1.backgroundColor = [UIColor redColor];    //设置标题    [button1 setTitle:@"开始" forState:UIControlStateNormal];    //添加一个点击事件    [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button1];    //添加一个放大按钮    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    button2.frame = CGRectMake(120, 400, 50, 20);    button2.tag = 102;    button2.backgroundColor = [UIColor redColor];    //设置标题    [button2 setTitle:@"放大" forState:UIControlStateNormal];    //添加一个点击事件    [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button2];            //添加一个缩小按钮    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    button3.frame = CGRectMake(190, 400, 50, 20);    button3.tag = 103;    button3.backgroundColor = [UIColor redColor];    //设置标题    [button3 setTitle:@"缩小" forState:UIControlStateNormal];    //添加一个点击事件    [button3 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button3];        return YES;}- (void)buttonAction:(UIButton *)button{    //开始旋转的按钮    if (button.tag == 101)    {        //开启定时器        [NSTimer scheduledTimerWithTimeInterval:1                                         target:self                                       selector:@selector(timeAction:)                                       userInfo:nil                                        repeats:YES];    }    else if (button.tag == 102)    {        for (UIView *view in _viewArrary)        {            <span style="color:#ff0000;">//放大</span>            view.transform = CGAffineTransformScale(view.transform, 2, 2);        }            }    else if (button.tag == 103)    {        for (UIView *view in _viewArrary)        {           <span style="color:#ff0000;"> //缩小</span>            view.transform = CGAffineTransformScale(view.transform, .5, .5);        }            }        }- (void)timeAction:(NSTimer *)time{    //开始动画    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:.5];        //遍历7个视图    for (int i=0; i<_viewArrary.count; i++)    {        UIView *view = [_viewArrary objectAtIndex:i];  //_viewArrary[i]        //设置背景颜色        view.backgroundColor = [UIColor colorWithRed:arc4random()%10*.1 green:arc4random()%10*.1 blue:arc4random()%10*.1 alpha:1];       <span style="color:#ff0000;"> //旋转</span>        view.transform = CGAffineTransformRotate(view.transform, M_PI/10);    }        //结束动画    [UIView commitAnimations];    _index ++;        //如果到第十秒停止    if (_index == 10)    {        for (UIView *view in _viewArrary)        {            [view removeFromSuperview];        }        //停止计时器        [time invalidate];    }}@end


0 0
原创粉丝点击