iOS Core Animation學習總結(3)--動畫的基本類型,iosanimation

來源:互聯網
上載者:User

iOS Core Animation學習總結(3)--動畫的基本類型,iosanimation

一. CABasicAnimation (基礎動畫)

移位:

    CABasicAnimation *animation = [CABasicAnimation animation];    //keyPath指定動畫類別,position表示移位    animation.keyPath = @"position";    //移動到x=200,y=200的位置    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];    animation.duration = 2;    //動畫執行完畢後不刪除動畫    animation.removedOnCompletion = NO;    //保持最新的狀態    animation.fillMode = @"forwards";    //添加動畫    [self.layer addAnimation:animation forKey:nil];

縮放:

    CABasicAnimation *animation = [CABasicAnimation animation];    //keyPath指定動畫類別,bounds表示縮放    animation.keyPath = @"bounds";    //縮放到width=50,height=50    animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 50, 50)];    animation.duration = 2;    //動畫完成不刪除動畫    animation.removedOnCompletion = NO;    //保持最新的狀態    animation.fillMode = @"forwards";        [self.layer addAnimation:animation forKey:nil];

旋轉:

    CABasicAnimation *animation = [CABasicAnimation animation];    //keyPath指定動畫類別,transform表示旋轉    animation.keyPath = @"transform";    //沿x,y軸順時針旋轉45度    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 1, 1, 0)];    animation.duration = 2;    animation.removedOnCompletion = NO;    animation.fillMode = @"forwards";        [self.layer addAnimation:animation forKey:nil];

 

二.  CAKeyframeAnimation (主要畫面格動畫)

    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];        anim.keyPath = @"position";    anim.removedOnCompletion = NO;    anim.fillMode = kCAFillModeForwards;    anim.duration = 2.0;        //設定圓形軌跡,並繞圓形軌跡移動    CGMutablePathRef path = CGPathCreateMutable();    CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));    anim.path = path;    CGPathRelease(path);            // 設定動畫的執行節奏    // kCAMediaTimingFunctionEaseInEaseOut : 一開始比較慢, 中間會加速,  臨近結束的時候, 會變慢    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    anim.delegate = self;    [anim setValue:@"aaa" forKey:@"TAG"];    [self.layer addAnimation:anim forKey:nil];

設定代理的回調方法,讓動畫結束後彈出提示

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{    NSString *strTag = [anim valueForKey:@"TAG"];    if ([strTag isEqualToString:@"aaa"]) {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Animation Done" message:@"動作完成" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil];        [alert show];    }}

 

三. CATransition(轉場動畫)

(1)視圖跳轉

    _newView = [[UIView alloc] init];    _newView.frame = CGRectMake(100, 100, 100, 100);    [self.view addSubview:_newView];        UIView *view1 = [[UIView alloc] init];    view1.frame = CGRectMake(0, 0, 100, 100);    view1.backgroundColor = [UIColor yellowColor];    [_newView addSubview:view1];        UIView *view2 = [[UIView alloc] init];    view2.frame = CGRectMake(0, 0, 100, 100);    view2.backgroundColor = [UIColor greenColor];    [_newView addSubview:view2];

添加轉場按鈕事件處理:

- (IBAction)exchangeView {    // 轉場動畫    CATransition *transition = [CATransition animation];    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    transition.type = @"pageCurl";    transition.subtype = kCATransitionFromRight;    transition.duration = 1;    [_newView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];    [_newView.layer addAnimation:transition forKey:@"myAnimation"];}

(2)控制器跳轉

- (IBAction)pushView {    CATransition *transition = [CATransition animation];    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];    //立體動畫效果    transition.type = @"cube";    [self.navigationController.view.layer addAnimation:transition forKey:@"navAnimation"];    TestViewController *testVC = [[TestViewController alloc] init];    [self.navigationController showViewController:testVC sender:nil];}

四. CAAnimationGroup (組合動畫)

    //添加圖片    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"curry.jpg"]];    imgView.frame = CGRectMake(100, 100, imgView.frame.size.width, imgView.frame.size.height);    [self.view addSubview:imgView];        //貝茲路徑路徑    UIBezierPath *movePath = [UIBezierPath bezierPath];    [movePath moveToPoint:CGPointMake(10.0, 10.0)];    [movePath addQuadCurveToPoint:CGPointMake(100, 300) controlPoint:CGPointMake(300, 100)];        //以下必須匯入QuartzCore包    //主要畫面格動畫(位置)    CAKeyframeAnimation * posAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];    posAnim.path = movePath.CGPath;    posAnim.removedOnCompletion = YES;        //縮放動畫    CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];    scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];    scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];    scaleAnim.removedOnCompletion = YES;        //透明動畫    CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];    opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];    opacityAnim.toValue = [NSNumber numberWithFloat:0.1];    opacityAnim.removedOnCompletion = YES;        //動畫組    CAAnimationGroup *animGroup = [CAAnimationGroup animation];    animGroup.animations = [NSArray arrayWithObjects:posAnim, scaleAnim, opacityAnim, nil];    animGroup.duration = 1;        [imgView.layer addAnimation:animGroup forKey:nil];

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.