xcode 不值钱的动画UIImageView

了解 animateWithDuration方法 制作动画变得不值钱

代码创建一个UIImageView 后加入self.view 容器中

调用点击屏幕touchesBegan 方法执行动画

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,weak)UIImageView * imgview;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CGFloat cgW=self.view.bounds.size.width;
    CGFloat imgW=174;
    CGFloat imgH=272;
    CGFloat cgY=(cgW-imgW)*0.5;
    
    UIImageView * imgType=[[UIImageView alloc]init];
    imgType.frame=CGRectMake(cgY, cgY, imgW, imgH);
    imgType.image=[UIImage imageNamed:@"timg"];
    imgType.contentMode=UIViewContentModeScaleAspectFill;
    
    self.imgview=imgType;
    self.imgview.alpha=1;//设置透明度
    [self.view addSubview:self.imgview];
  
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    //动画1
//    [self animationPlay1];
    //动画2
//    [self animationPlay2];
    //动画3
//    [self animationPlay3];
    //动画4
    [self animationPlay4];
}

/**
 动画一
 向下滑动
 并且消失
 */
-(void)animationPlay1{
    /**
     animateWithDuration:执行动画的时间
     animations:执行的代码
     */
    [UIView animateWithDuration:2.0 animations:^{
        CGRect temp=self.imgview.frame;
        temp.origin.y+=200;
        self.imgview.alpha=0.1;//设置透明度
        self.imgview.frame=temp;
    }];
}
/**
 向下滑动然后回去
 */
-(void)animationPlay2{
    /**
     animateWithDuration:执行动画的时间
     animations:执行的代码
     completion:执行完代码的回调函数
     */
[UIView animateWithDuration:1.5 animations:^{
    CGRect temp=self.imgview.frame;
    temp.origin.y+=100;
    self.imgview.frame=temp;


} completion:^(BOOL finished) {
    CGRect temp=self.imgview.frame;
    temp.origin.y-=100;
    self.imgview.frame=temp;

}];
}
/**
 向下滑动然后回去延迟1秒钟
 */
-(void)animationPlay3{
    /**
     animateWithDuration: 动画时间
     delay:延迟时间
     options:kNilOptions 这个一般设置为kNilOptions 也就是0
     animations:要执行的动画代码
     completion:回调函数
     */
[UIView animateWithDuration:1.5 delay:1.0 options:kNilOptions animations:^{
    CGRect temp=self.imgview.frame;
    temp.origin.y+=100;
    self.imgview.frame=temp;
} completion:^(BOOL finished) {
    CGRect temp=self.imgview.frame;
    temp.origin.y-=100;
    self.imgview.frame=temp;
}];

}
/**
 放大图片
 然后缩放回去
 */
-(void) animationPlay4{
    CGFloat cgW=self.view.bounds.size.width;
    //    CGFloat cgH=self.view.bounds.size.height;
    
    CGFloat imgW=self.imgview.bounds.size.width;
    CGFloat cgY=(cgW-imgW)*0.5;
    [UIView animateWithDuration:1.5 animations:^{
        CGRect temp=self.imgview.frame;
        temp=CGRectMake(0, cgY, self.view.bounds.size.width, self.view.frame.size.height-cgY);
        self.imgview.frame=temp;
        self.imgview.alpha=0.0;//设置透明度
       
    } completion:^(BOOL finished) {
       [UIView animateWithDuration:2.0 animations:^{
           CGFloat cgW=self.view.bounds.size.width;
           CGFloat imgW=174;
           CGFloat imgH=272;
           CGFloat cgY=(cgW-imgW)*0.5;
//           CGRect temp=self.imgview.frame;
//           temp=CGRectMake(cgY, cgY, imgW, imgH);
           self.imgview.alpha=1.0;//设置透明度
           self.imgview.frame=CGRectMake(cgY, cgY, imgW, imgH);
       }];
    }];

    
}

@end
View Code
原文地址:https://www.cnblogs.com/fleas/p/5561551.html