UIView动画

来源:互联网 发布:按键精灵 数据库插件 编辑:程序博客网 时间:2024/05/01 18:10

1、普通动画

#import "ViewController.h"@interface ViewController (){    UIImageView * _imageView;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    self.view.backgroundColor = [UIColor whiteColor];        UIImage * image = [UIImage imageNamed:@"dog.jpg"];    _imageView = [[UIImageView alloc] initWithImage:image];    _imageView.center = CGPointMake(_imageView.frame.size.width/2, _imageView.frame.size.height/2);    [self.view addSubview:_imageView];}-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    //重新设置图片位置是为了重复触摸屏幕可以重复多吃动画效果    _imageView.center = CGPointMake(_imageView.frame.size.width/2, _imageView.frame.size.height/2);        [UIView beginAnimations:nil context:NULL];        //设置动画持续时间    [UIView setAnimationDuration:1.0];        //设置动画延迟1秒后开始执行    [UIView setAnimationDelay:1.0];        //设置动画重复3次    [UIView setAnimationRepeatCount:3];        //改变图片位置    _imageView.center = CGPointMake(250, 500);        //提交动画    [UIView commitAnimations];    }

2、透明化与动画弧

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    //重新设置图片位置是为了重复触摸屏幕可以重复多吃动画效果    _imageView.center = CGPointMake(_imageView.frame.size.width/2, _imageView.frame.size.height/2);        //改变透明度    _imageView.alpha = 1.0;        [UIView beginAnimations:nil context:NULL];        //设置动画持续时间    [UIView setAnimationDuration:1.0];        //设置动画延迟1秒后开始执行    [UIView setAnimationDelay:1.0];        //设置动画重复3次    [UIView setAnimationRepeatCount:3];        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];        //改变图片位置    _imageView.center = CGPointMake(250, 500);        _imageView.alpha = 0.0;        //提交动画    [UIView commitAnimations];    }

其中动画弧的效果为:

typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {    //开始后速度慢慢加快,中间处开始速度慢慢降低    UIViewAnimationCurveEaseInOut,         // slow at beginning and end        //速度慢慢变快    UIViewAnimationCurveEaseIn,            // slow at beginning    //速度慢慢变慢    UIViewAnimationCurveEaseOut,           // slow at end    //匀速    UIViewAnimationCurveLinear};

3、放大、缩小、旋转

    [UIView beginAnimations:nil context:NULL];        //第一个2是x方向上扩大一倍,第二个是y方向上扩大一倍    CGAffineTransform transformScale = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);        //控制旋转    CGAffineTransform transformRotate = CGAffineTransformRotate(CGAffineTransformIdentity, M_1_PI);            _imageView.transform = CGAffineTransformConcat(transformScale, transformRotate);        [UIView commitAnimations];


具体其他动画请参考博文:http://blog.csdn.net/lvdezhou/article/details/49660213

4、动画复原效果

此效果为:动画执行完后,再以动画方式复原,然后立马显示原动画的最终效果

    [UIView beginAnimations:nil context:NULL];            //设置动画延迟1秒后开始执行    [UIView setAnimationDelay:1.0];    //设置动画持续时间3秒,是为了能够慢动作看清楚动画    [UIView setAnimationDuration:3.0];            //设置动画复原效果    [UIView setAnimationRepeatAutoreverses:YES];        //第一个2是x方向上扩大一倍,第二个是y方向上扩大一倍    CGAffineTransform transformScale = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);        //控制旋转    CGAffineTransform transformRotate = CGAffineTransformRotate(CGAffineTransformIdentity, M_1_PI);            _imageView.transform = CGAffineTransformConcat(transformScale, transformRotate);                [UIView commitAnimations];

下面图片录的不怎么样,但还是能看出来效果的:


5、状态监视:

监测动画的开始/结束时机

    [UIView beginAnimations:nil context:NULL];            //设置动画延迟1秒后开始执行    [UIView setAnimationDelay:1.0];           [UIView setAnimationDuration:3.0];        [UIView setAnimationDelegate:self];    [UIView setAnimationWillStartSelector:@selector(startActionWithAnimationID:withContext:)];    [UIView setAnimationDidStopSelector:@selector(stopActionWithAnimationID:withFinished:withContext:)];            [UIView setAnimationRepeatAutoreverses:YES];        //第一个2是x方向上扩大一倍,第二个是y方向上扩大一倍    CGAffineTransform transformScale = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);        //控制旋转    CGAffineTransform transformRotate = CGAffineTransformRotate(CGAffineTransformIdentity, M_1_PI);            _imageView.transform = CGAffineTransformConcat(transformScale, transformRotate);               [UIView commitAnimations];

下面是两个监视函数:

- (void) startActionWithAnimationID:(NSString *)animationID withContext:(void *) context {    NSLog(@"%@---%p", animationID, context);    NSLog(@"%s", __func__);}- (void) stopActionWithAnimationID:(NSString *)animationID withFinished:(NSNumber *)finished withContext:(void *) context {    //其中finished可以辨别动画是否正常结束,其余两个参数都是[UIView beginAnimations:nil context:NULL]中的值    NSLog(@"%@---%@---%p", animationID, finished, context);    NSLog(@"%s", __func__);}
6、过渡动画

typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {    UIViewAnimationTransitionNone,    UIViewAnimationTransitionFlipFromLeft,//从左向右翻转    UIViewAnimationTransitionFlipFromRight,//从右到左翻转    UIViewAnimationTransitionCurlUp,//向下向上翻页    UIViewAnimationTransitionCurlDown,//从上向下翻页};    UIViewAnimationTransition transition = UIViewAnimationTransitionFlipFromLeft;//设置动画的时候添加:    [UIView setAnimationTransition:transition forView:_imageView cache:YES];



0 0
原创粉丝点击