标签:实衰减动画
@interface ViewController ()
@property (nonatomic,strong)CADisplayLink *displayLink;
@property (nonatomic) NSInteger count;
@end
- (void)viewDidLoad {
[superviewDidLoad];
self.displayLink = [CADisplayLinkdisplayLinkWithTarget:self
selector:@selector(displayLinkEvent:)];
[self performSelector:@selector(eventOne)
withObject:nil
afterDelay:1];
[self performSelector:@selector(eventTwo)
withObject:nil
afterDelay:2];
}
- (void)displayLinkEvent:(id)obj
{
self.count ++;
NSLog(@"count = %ld",(long)self.count);
}
- (void)eventOne{
[self.displayLinkaddToRunLoop:[NSRunLoopcurrentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)eventTwo{
[self.displayLinkinvalidate];
}
- (void)accessNormalLayer{
self.normalLayer = [CALayerlayer];
self.normalLayer.frame =CGRectMake(100,100, 100, 100);
self.normalLayer.backgroundColor = [UIColorredColor].CGColor;
[self.view.layeraddSublayer:self.normalLayer];
//
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
basicAnimation.fromValue = [NSValue valueWithCGPoint:self.normalLayer.position];
basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(100 +50,400)];
basicAnimation.duration = 4.0;
basicAnimation.timingFunction = \
[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];
// layer的postion 相当于view 的center
self.normalLayer.position =CGPointMake(100 +50,400);
[self.normalLayeraddAnimation:basicAnimation forKey:nil];
//1.4秒移除后,动画直接到终点
[self performSelector:@selector(remoVeNormalAnimation)withObject:nilafterDelay:1.4];
}
- (void)remoVeNormalAnimation{
CALayer *layer = self.normalLayer.presentationLayer;
NSLog(@"%@",NSStringFromCGRect(layer.frame));
NSLog(@"%@",NSStringFromCGRect(self.normalLayer.frame));
[self.normalLayerremoveAllAnimations];
}
- (void)handlePanGesture:(UIPanGestureRecognizer *)recognizer{
//获取定位点
CGPoint translation = [recognizer translationInView:self.view];
//recognizer.view.center 指的是button
recognizer.view.center =CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y +translation.y);
//让他恢复坐标系
[recognizer setTranslation:CGPointMake(0,0) inView:self.view];
if (recognizer.state ==UIGestureRecognizerStateChanged) {
NSLog(@"手势停止时执行这一句话");
//获取加速度
CGPoint velocity = [recognizer velocityInView:self.view];
//初始化POP的deacy(衰减)动画
POPDecayAnimaton *decayAnimation = \
[POPDecayAnimation animationWithPropertyName:kPOPLayerPosition];
decayAnimation.velocity = [NSValue valueWithCGPoint:velocity];
[recognizer.view.layer pop_addAnimation:decayAnimation forKey:nil];
}
}
- (void)buttonEvent:(UIButton *)button
{
//[button.layer pop_removeAllAnimations];
}
- (void)viewDidLoad {
[superviewDidLoad];
self.button = [[UIButtonalloc]initWithFrame:CGRectMake(100,100, 100, 100)];
self.button .backgroundColor = [UIColorredColor];
self.button.layer.cornerRadius = 50;
self.button.layer.masksToBounds = YES;
self.button.center =self.view.center;
[self.viewaddSubview:self.button];
[self.buttonaddTarget:self
action:@selector(buttonEvent:)
forControlEvents:UIControlEventTouchDragInside];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlePanGesture:)];
[self.buttonaddGestureRecognizer:panGesture];
版权声明:本文为博主原创文章,未经博主允许不得转载。
使用 Facebook 开源动画库 POP 实现真实衰减动画
标签:实衰减动画
原文地址:http://blog.csdn.net/baitxaps/article/details/46899707