UIVIiew and animation
程序员文章站
2024-03-24 12:21:16
...
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initButton];
}
-(void)initView{
for (int i=0; i < 4; i ++ ) {
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(30+((self.view.frame.size.width-150)/4+30)*i, 500, (self.view.frame.size.width-150)/4, (self.view.frame.size.width-150)/4)];
view.backgroundColor =[UIColor redColor];
view.layer.cornerRadius = (self.view.frame.size.width-150)/4/2;
[UIView animateWithDuration:4 delay:1 usingSpringWithDamping:0.01 initialSpringVelocity:0.1 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
view.transform = CGAffineTransformMakeScale(1.0 , 1.0);
view.center = CGPointMake(30+((self.view.frame.size.width-150)/4+30)*i, 800);
} completion:^(BOOL finished){
view.center = CGPointMake(30+((self.view.frame.size.width-150)/4+30)*i, 800);
}];
[self.view addSubview:view];
}
}
//duration动画持续时间
//delay动画延时几秒执行
//velocity动画开始速度
//options动画效果参数options:UIViewAnimationOptionBeginFromCurrentState
//completion动画执行完成的回调
//usingSpringWithDamping:0.01范围0.01-1。0取值越小,弹性越明显
-(void)initButton{
UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 25, self.view.frame.size.height-50, 50, 50)];
button.backgroundColor =[UIColor redColor];
[button addTarget:self action:@selector(initView) forControlEvents:UIControlEventTouchDown ];
NSLog(@"%d",1);
[self.view addSubview:button];
}
optiona后面所接的选择
enum {
UIViewAnimationOptionLayoutSubviews = 1 << 0,
UIViewAnimationOptionAllowUserInteraction = 1 << 1,
UIViewAnimationOptionBeginFromCurrentState = 1 << 2,
UIViewAnimationOptionRepeat = 1 << 3,
UIViewAnimationOptionAutoreverse = 1 << 4,
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5,
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6,
UIViewAnimationOptionAllowAnimatedContent = 1 << 7,
UIViewAnimationOptionShowHideTransitionViews = 1 << 8,
UIViewAnimationOptionOverrideInheritedOptions = 1 << 9,
UIViewAnimationOptionCurveEaseInOut = 0 << 16,
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,
UIViewAnimationOptionTransitionNone = 0 << 20,
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
UIViewAnimationOptionTransitionCurlUp = 3 << 20,
UIViewAnimationOptionTransitionCurlDown = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,
};
关于 view.transform = CGAffineTransformMakeScale(1.0 , 1.0);
1.CGAffineTransformMakeScale (CGFloat sx, CGFloat sy);//创建一个给定比例放缩的变换,那么图片的宽度就会变为 width*sx ,对应高度变为 hight * sy。
2.CGAffineTransform CGAffineTransformMakeRotation ( CGFloat angle); //创建一个旋转角度的变化在这里可以看到参数并不是一个角度,但是它是把参数作为一个弧度,然后把弧度再转换为角度来处理,其结果就可能是将一个图片视图旋转了多少度。
3。CGAffineTransform CGAffineTransformMakeTranslation (CGFloat tx,CGFloat ty);这个就比较好理解了,假设是一个视图,那么它的起始位置 x 会加上tx , y 会加上 ty