基礎(chǔ)知識
1.CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty)(平移:設(shè)置平移量)
2.CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)(縮放:設(shè)置縮放比例)僅通過設(shè)置縮放比例就可實現(xiàn)視圖撲面而來和縮進頻幕的效果膜眠。
3.CGAffineTransformMakeRotation(CGFloat angle)(旋轉(zhuǎn):設(shè)置旋轉(zhuǎn)角度)
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通過矩陣變換之后的區(qū)域
*/
github地址:https://github.com/Dzhijian/ZJAnimationDemo.git
代碼實現(xiàn)
#######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:@"位移",@"縮放",@"旋轉(zhuǎn)",@"組合",@"反轉(zhuǎn)", 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 - 旋轉(zhuǎn) 旋轉(zhuǎn)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 - 反轉(zhuǎn)
-(void)invertAnimation{
_imageView.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:1.0f animations:^{
//反轉(zhuǎn)
_imageView.transform = CGAffineTransformInvert(CGAffineTransformMakeScale(11, 11));
}];
}
@end