iOS开发之Animation简单使用基础详解

基础知识

1.CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty)(平移:设置平移量)

2.CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)(缩放:设置缩放比例)仅通过设置缩放比例就可实现视图扑面而来和缩进频幕的效果。

3.CGAffineTransformMakeRotation(CGFloat angle)(旋转:设置旋转角度)

CGAffineTransformIdentity : 单位矩阵变换,一般用于仿射变换的初始化或者还原。
CGAffineTransformEqualToTransform(CGAffineTransform t1,
CGAffineTransform t2) : 判断两个变换矩阵是否相等

CGAffineTransformConcat(CGAffineTransform t1,
CGAffineTransform t2) : 得到两个矩阵相加后得到的最终矩阵

CGPoint CGPointApplyAffineTransform(CGPoint point,
CGAffineTransform t) : 某点通过矩阵变换之后的点

CGSize CGSizeApplyAffineTransform(CGSize size, CGAffineTransform t) : 某个size通过矩阵变换之后的size

CGRect CGRectApplyAffineTransform(CGRect rect, CGAffineTransform t) : 某个Rect通过矩阵变换之后的区域
*/

github地址:https://github.com/Dzhijian/ZJAnimationDemo.git

代码实现

#######import "ZJViewAnimationController.h"
@interface ZJViewAnimationController ()
@property(nonatomic, strong) UIImageView *imageView;
@end

@implementation ZJViewAnimationController
-(void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Position";
    [self setUpAllView];  
}
-(void)setUpAllView{
 self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2 - 50, 160, ViewWH, ViewWH)];
    [self.view addSubview:_imageView];
    _imageView.image = [UIImage imageNamed:@"745"];
     NSArray *titleArr = [NSArray arrayWithObjects:@"位移",@"缩放",@"旋转",@"组合",@"反转", nil];
    for (NSInteger i = 0 ; i < titleArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(20+(ScreenWidth-100)/4*(i%4)+20*(i%4), ScreenHeight - 240 +60*(i/4), (ScreenWidth-100)/4, 40);
        btn.layer.cornerRadius = 6;
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        btn.backgroundColor = [UIColor orangeColor];
        btn.tag = i;
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
        [self.view addSubview:btn];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
}

-(void)btnAction:(UIButton *)sender{
switch (sender.tag) {
        case 0:
            [self positionAnimation];
            break;
        case 1:
            [self scaleAnimation];
            break;
        case 2:
            [self rotationAnimation];
            break;
        case 3:
            [self combinationAnimation];
            break;
        case 4:
            [self  invertAnimation];
            break;
        default:
            break;
    }
}
#######pragma mark -  位移  x轴移动100移 y轴移动80
-(void)positionAnimation{
    _imageView.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:1.0f animations:^{
        _imageView.transform = CGAffineTransformMakeTranslation(100, 80);
    }];
}

#######pragma mark -  缩放  放大两倍
-(void)scaleAnimation{
    _imageView.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:1.0f animations:^{
        _imageView.transform = CGAffineTransformMakeScale(2, 2);
    }];
}

#######pragma mark -  旋转  旋转180°
-(void)rotationAnimation{
    _imageView.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:1.0f animations:^{
        _imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    }];
}

#######pragma mark -  组合动画
-(void)combinationAnimation{
    _imageView.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:1.0f animations:^{
        CGAffineTransform transform1 = CGAffineTransformMakeRotation(M_PI);
        CGAffineTransform transform2 = CGAffineTransformScale(transform1, 0.5, 0.5);
        _imageView.transform = CGAffineTransformTranslate(transform2, -200, 0);
    }];
}

#######pragma mark -  反转
-(void)invertAnimation{
    _imageView.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:1.0f animations:^{
        //反转
        _imageView.transform = CGAffineTransformInvert(CGAffineTransformMakeScale(11, 11));
    }];
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容