#import "ViewController.h" #import "POP.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(log)]; // [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; return; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 50, 50)]; label.backgroundColor = [UIColor redColor]; [self.view addSubview:label]; //核心动画 CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; //keypath类型要和fromValue、toValue类型匹配 // ani.fromValue // ani.toValue = [NSValue valueWithCGRect:CGRectMake(200, 300, 50, 50)]; //动画目标值 ani.toValue = @(3); //动画时长 ani.duration = 3; //动画结束是否移除 ani.removedOnCompletion = NO; ani.fillMode = kCAFillModeForwards; //自动变回去 ani.autoreverses = YES; //添加动画 [label.layer addAnimation:ani forKey:@"qwe"]; //通过key值取到animation // label.layer animationForKey:<#(NSString *)#> //延时触发方法 // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // NSLog(@"%@",NSStringFromCGRect(label.layer.frame)); // }); // [self performSelector:@selector(log) withObject:nil afterDelay:4]; } - (void)log { NSLog(@"delay!"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)spring:(id)sender { //弹性动画 //iOS7.0后新出的弹性动画,Damping阻尼,Velocity速度 // [UIView animateWithDuration:3 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:2.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ // [sender setFrame:CGRectMake(200, 100, 80, 40)]; // } completion:nil]; //frame变化 POPSpringAnimation *spring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame]; spring.toValue = [NSValue valueWithCGRect:CGRectMake(230, 40, 80, 40)]; //动画代理(代理函数包括:动画开始,动画结束,动画到达目标值、动画应用到控件上) // spring.delegate = self; //缩放 // POPSpringAnimation *spring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY]; //// spring.fromValue默认从当前位置开始 // spring.fromValue = [NSValue valueWithCGSize:CGSizeMake(1, 1)]; // spring.toValue = [NSValue valueWithCGSize:CGSizeMake(3, 3)]; //背景色变化 // POPSpringAnimation *spring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewBackgroundColor]; // spring.toValue = [NSValue valueWithCGRect:CGRectMake(0.1, 0.2, 0.3, 1.0)]; //设置振幅 spring.springBounciness = 20; //振幅速度 spring.springSpeed = 20; // spring.dynamicsMass = 100; // spring.dynamicsTension = 100; // spring.dynamicsFriction = 100; [sender pop_addAnimation:spring forKey:@"spring"]; } //减速动画 - (IBAction)decay:(id)sender { // POPDecayAnimation *decay = [POPDecayAnimation animationWithPropertyNamed:kPOPViewCenter]; //减速不能设置toValue // decay.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 100)]; // decay.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)]; // decay.velocity = [NSValue valueWithCGPoint:CGPointMake(200, 100)]; //缩放 POPDecayAnimation *decay = [POPDecayAnimation animationWithPropertyNamed:kPOPViewScaleXY]; decay.velocity = [NSValue valueWithCGSize:CGSizeMake(4, 4)]; [sender pop_addAnimation:decay forKey:@"decay"]; } - (IBAction)basic:(id)sender { // [POPBasicAnimation animationWithPropertyNamed:kPOPViewFrame]; POPBasicAnimation *basic = [POPBasicAnimation animation]; basic.property = [POPAnimatableProperty propertyWithName:kPOPViewFrame]; basic.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 80, 40)]; basic.toValue = [NSValue valueWithCGRect:CGRectMake(200, 200, 80, 40)]; //动画时长 basic.duration = 2; [sender pop_addAnimation:basic forKey:@"basic"]; } //自定义 - (IBAction)Custom:(id)sender { //block返回值为yes代表动画一直执行(block会继续执行),返回值no代表动画结束(block不会继续被执行)。 // POPCustomAnimation *cus = [POPCustomAnimation animationWithBlock:^BOOL(id target, POPCustomAnimation *animation) { // // //修改alpha值 // CGFloat alpha = [target alpha]; // alpha -= 0.005; // [target setAlpha:alpha]; // // //每次向右移动1 // CGRect frame = [target frame]; // frame.origin.x += 1; // [target setFrame:frame]; // //如果x坐标大于等于200,则结束动画,否则继续动画 // if (frame.origin.x >= 200) { // return NO; // } // return YES; // }]; // [sender pop_addAnimation:cus forKey:@"custom"]; //贝塞尔曲线创建圆路径(参数顺序:圆心、半径、起始角度、结束角度、是否顺时针绘制) UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 200) radius:80 startAngle:M_PI endAngle:M_PI*(7/2) clockwise:YES]; //移动到某个点 [path moveToPoint:CGPointMake(120, 200)]; //添加到另外一个点之间的线 [path addLineToPoint:CGPointMake(180, 250)]; //再次移动到某个点 [path moveToPoint:CGPointMake(180, 250)]; [path addLineToPoint:CGPointMake(250, 140)]; //渲染路径的特殊layer CAShapeLayer *layer = [CAShapeLayer layer]; //设置渲染路径 layer.path = path.CGPath; //线条颜色 layer.strokeColor = [[UIColor redColor]CGColor]; //填充颜色 layer.fillColor = [[UIColor whiteColor]CGColor]; //线条宽度 layer.lineWidth = 5; //添加到当前页面的layer上 [self.view.layer addSublayer:layer]; //核心动画 // CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; // animation.toValue = @(layer.strokeEnd); // animation.fromValue = @(layer.strokeStart); // animation.duration = 2; // [layer addAnimation:animation forKey:@"coreAnimation"]; // return; //popcustom动画 POPCustomAnimation *layCus = [POPCustomAnimation animationWithBlock:^BOOL(id target, POPCustomAnimation *animation) { CAShapeLayer *shapeLayer = target; shapeLayer.strokeEnd += 0.01; if (shapeLayer.strokeEnd >= 1) { return NO; } return YES; }]; layer.strokeEnd = 0; [layer pop_addAnimation:layCus forKey:@"custom"]; } - (IBAction)property:(id)sender { //自定义property POPBasicAnimation *animation = [POPBasicAnimation animation]; //设置动画规则 animation.property = [POPAnimatableProperty propertyWithName:@"customProperty" initializer:^(POPMutableAnimatableProperty *prop) { //从values数组中取值显示到target上 [prop setWriteBlock:^(id target, const CGFloat *values) { [target setAlpha:values[0]]; }]; //从target取值赋值到values数组中 [prop setReadBlock:^(id target, CGFloat *values) { values[0] = [target alpha]; }]; }]; // //让button变成透明 // animation.toValue = @(0); // animation.property = [POPAnimatableProperty propertyWithName:@"increase" initializer:^(POPMutableAnimatableProperty *prop) { // // [prop setWriteBlock:^(id target, const CGFloat *values) { // [target setTitle:[NSString stringWithFormat:@"%.f",values[0]] forState:UIControlStateNormal]; // }]; // // [prop setReadBlock:^(id target, CGFloat *values) { // values[0] = [target titleLabel].text.floatValue; // }]; // }]; // animation.toValue = @(1000); // animation.property = [POPAnimatableProperty propertyWithName:@"size" initializer:^(POPMutableAnimatableProperty *prop) { // //writeblock和readblock正好是相反的两个过程 // [prop setWriteBlock:^(id target, const CGFloat *values) { // // CGRect frame = [target frame]; // frame.size.width = values[0]; // frame.size.height = values[1]; // [target setFrame:frame]; // // }]; // // [prop setReadBlock:^(id target, CGFloat *values) { // CGRect frame = [target frame]; // values[0] = frame.size.width; // values[1] = frame.size.height; // }]; // }]; // animation.fromValue = [NSValue valueWithCGSize:CGSizeMake(80, 40)]; // animation.toValue = [NSValue valueWithCGSize:CGSizeMake(200, 200)]; // animation.property = [POPAnimatableProperty propertyWithName:@"qwer" initializer:^(POPMutableAnimatableProperty *prop) { // [prop setReadBlock:^(id target, CGFloat *values) { // CGRect frame = [target frame]; // values[0] = frame.origin.x; // values[1] = frame.origin.y; // values[2] = [target titleLabel].text.floatValue; // values[3] = 0; // }]; // // [prop setWriteBlock:^(id target, const CGFloat *values) { // CGRect frame = [target frame]; // NSLog(@"%f-%f-%f-%f",values[0],values[1],values[2],values[3]); // frame.origin.x = values[0]; // frame.origin.y = values[1]; // [target setFrame:frame]; // [target setTitle:[NSString stringWithFormat:@"%.f",values[2]] forState:UIControlStateNormal]; // }]; // }]; // animation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 0, 0)]; // animation.toValue = [NSValue valueWithCGRect:CGRectMake(300, 300, 1000, 0)]; //设置动画时长 animation.duration = 2; [sender pop_addAnimation:animation forKey:@"animation"]; } @end