ios 果冻动画实现

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.height)];

self.contentView.backgroundColor = [UIColor brownColor];

[self.view addSubview:self.contentView];

_centerPoint = self.contentView.center;

self.shapeLayer = [CAShapeLayer layer];

self.shapeLayer.frame = self.contentView.frame;

self.shapeLayer.fillColor = [UIColor redColor].CGColor;

self.shapeLayer.path = [self calculatePathWithPoint:CGPointMake(self.view.width/2, self.view.height/2)].CGPath;

[self.contentView.layer addSublayer:self.shapeLayer];

self.pointView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 5)];

self.pointView.backgroundColor = [UIColor greenColor];

self.pointView.center = self.contentView.center;

[self.contentView addSubview:self.pointView];

self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkEvent)];

[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureEvent:)];

[self.contentView addGestureRecognizer:panGesture];

}

- (UIBezierPath *)calculatePathWithPoint:(CGPoint)point {

UIBezierPath *path = [UIBezierPath bezierPath];

[path moveToPoint:CGPointMake(0, self.view.height/2)];

[path addLineToPoint:CGPointMake(0, self.view.height)];

[path addLineToPoint:CGPointMake(self.view.width, self.view.height)];

[path addLineToPoint:CGPointMake(self.view.width, self.view.height/2)];

[path addQuadCurveToPoint:CGPointMake(0, self.view.height/2) controlPoint:point];

return path;

}

- (void)displayLinkEvent {

CALayer *layer = self.pointView.layer.presentationLayer;

self.shapeLayer.path = [self calculatePathWithPoint:layer.position].CGPath;

}

- (void)panGestureEvent:(UIPanGestureRecognizer *)panGesture {

CGPoint point = [panGesture locationInView:panGesture.view];

NSLog(@"%@",NSStringFromCGRect(panGesture.view.frame));

//    NSLog(@"point===%@",NSStringFromCGPoint(point));

CGPoint calculatePoint = CGPointMake((point.x + _centerPoint.x)/2, (point.y + _centerPoint.y)/2);

//    NSLog(@"calculatePoint===%@",NSStringFromCGPoint(calculatePoint));

if (panGesture.state == UIGestureRecognizerStateChanged) {

self.shapeLayer.path = [self calculatePathWithPoint:calculatePoint].CGPath;

self.pointView.center = calculatePoint;

}else if (panGesture.state == UIGestureRecognizerStateEnded ||

panGesture.state == UIGestureRecognizerStateFailed ||

panGesture.state == UIGestureRecognizerStateCancelled) {

[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.25f initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

self.pointView.center = self.contentView.center;

} completion:^(BOOL finished) {

self.pointView.center = self.contentView.center;

}];

}

}

- (void)viewWillDisappear:(BOOL)animated {

[self.displayLink invalidate];

[super viewWillDisappear:animated];

}

代码地址:果冻效果 


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Core Animation Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,...
    45b645c5912e阅读 8,225评论 0 21
  • iOS动画篇之CoreAnimation动画 9月 22, 2016发布在Objective-C App如果想被大...
    白水灬煮一切阅读 6,616评论 0 0
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,550评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,727评论 6 30
  • 必定要望向窗外的等待今夜就像那些除却你我的一生 写诗的手,拂拭心头的尘描眉的笔,搁浅裙角无名的绣花终为岁月合身: ...
    2020号阅读 3,961评论 25 59

友情链接更多精彩内容