iOS 神奇的果冻效果

 

//
//  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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值