码迷,mamicode.com
首页 > 移动开发 > 详细

iOS的果冻效果

时间:2016-01-06 20:00:11      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

具体使用的CADisplayLink和贝塞尔曲线

效果:

技术分享

//
//  DisplayView.m
//  CustomAnimation
//
//  Created by LV on 16/1/6.
//  Copyright © 2016年 Wieye. All rights reserved.
//

#import "DisplayView.h"

@interface DisplayView ()

@property (nonatomic, strong) CADisplayLink * displayLink;

@property (nonatomic, assign) CGFloat to;
@property (nonatomic, assign) CGFloat from;

@end

@implementation DisplayView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        NSLog(@"Init");
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
     NSLog(@"DrawRect");
    
    [[UIColor purpleColor] setFill];
    CALayer *layer = self.layer.presentationLayer;
    CGFloat progress    = 1 - (layer.position.y - self.to) / (self.from - self.to);
    CGFloat height      = CGRectGetHeight(rect);
    CGFloat deltaHeight = height / 2 * (0.5 - fabs(progress - 0.5));
    CGPoint topLeft     = CGPointMake(0, deltaHeight);
    CGPoint topRight    = CGPointMake(CGRectGetWidth(rect), deltaHeight);
    CGPoint bottomLeft  = CGPointMake(0, height);
    CGPoint bottomRight = CGPointMake(CGRectGetWidth(rect), height);

    UIBezierPath* path = [UIBezierPath bezierPath];
    [path moveToPoint:topLeft];
    [path addQuadCurveToPoint:topRight controlPoint:CGPointMake(CGRectGetMidX(rect), 0)];
    [path addLineToPoint:bottomRight];
    [path addQuadCurveToPoint:bottomLeft controlPoint:CGPointMake(CGRectGetMidX(rect), height - deltaHeight)];
    [path closePath];
    [path fill];
}

#pragma mark - Public Action

- (void)startAnimationFrom:(CGFloat)from to:(CGFloat)to
{
    if (self.displayLink == nil)
    {
        NSLog(@"StartAnimation");
        
        self.from = from;
        self.to   = to;
        self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
        [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    }

    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0 options:0 animations:^{
        self.center = CGPointMake(self.center.x, self.to);
    } completion:^(BOOL finished)
     {
         [self stopAnimation];
     }];
}

- (void)stopAnimation
{
    if (self.displayLink != nil)
    {
        [self.displayLink invalidate];
        self.displayLink = nil;
    }
}

#pragma mark - Private Action

- (void)tick:(CADisplayLink *)displayLink
{
    [self setNeedsDisplay];
}

@end
#import <UIKit/UIKit.h>

@interface DisplayView : UIView

- (void)startAnimationFrom:(CGFloat)from to:(CGFloat)to;

- (void)stopAnimation;

@end

 

iOS的果冻效果

标签:

原文地址:http://www.cnblogs.com/R0SS/p/5106477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!