美文网首页iOS DeveloperiOS开发
iOS 神奇的果冻效果

iOS 神奇的果冻效果

作者: LYSNote | 来源:发表于2017-01-12 15:02 被阅读445次
果冻效果.gif
//
//  ViewController.m
//  AnimationDemo
//
//  Created by HENAN on 16/12/27.
//  Copyright © 2016年 LYS. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic,strong)CAShapeLayer *shareLayer;
@property (nonatomic,strong)CADisplayLink *displayLink;
@property (nonatomic,assign)CGPoint centerPoint;
@property (nonatomic,strong)UIView *controlView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.centerPoint = CGPointMake(CGRectGetWidth(self.view.frame) / 2.0, CGRectGetHeight(self.view.frame) / 2.0);
    self.controlView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    _controlView.center = self.centerPoint;
    _controlView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_controlView];
    
    self.shareLayer = [CAShapeLayer layer];
    _shareLayer.frame = self.view.bounds;
    _shareLayer.path = [self calculatePathWithPoint:self.controlView.center].CGPath;
    _shareLayer.fillColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:_shareLayer];
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkEvent)]; [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureEvent:)];
    panGesture.delegate = self;
    [self.view addGestureRecognizer:panGesture];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    CGPoint point = [gestureRecognizer locationInView:gestureRecognizer.view];
    if (point.y < (CGRectGetHeight(self.view.frame) / 2.0 - 30) || point.y > (CGRectGetHeight(self.view.frame) / 2.0 + 30)) {
        return NO;
    }else {
        return YES;
    }
}
- (UIBezierPath *)calculatePathWithPoint:(CGPoint)point {
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, CGRectGetHeight(self.view.frame) / 2.0)];
    [path addLineToPoint:CGPointMake(0, CGRectGetHeight(self.view.frame))];
    [path addLineToPoint:CGPointMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
    [path addLineToPoint:CGPointMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) / 2.0)];
    [path addQuadCurveToPoint:CGPointMake(0, CGRectGetHeight(self.view.frame) / 2.0)
                 controlPoint:point];
    
    return path;
}
- (void)panGestureEvent:(UIPanGestureRecognizer *)panGesture {
    CGPoint point = [panGesture locationInView:panGesture.view];
    if (panGesture.state == UIGestureRecognizerStateChanged) {
        self.controlView.center = point;
    } else if (panGesture.state == UIGestureRecognizerStateCancelled ||
               panGesture.state == UIGestureRecognizerStateEnded ||
               panGesture.state == UIGestureRecognizerStateFailed) {
        
        [UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.25f initialSpringVelocity:0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.controlView.center = self.centerPoint;
                         } completion:nil];
    }
}
- (void)displayLinkEvent {
    self.shareLayer.path = [self calculatePathWithPoint:self.controlView.layer.presentationLayer.position].CGPath;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

相关文章

网友评论

    本文标题:iOS 神奇的果冻效果

    本文链接:https://www.haomeiwen.com/subject/hygdbttx.html