欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實現(xiàn)貝塞爾曲線動畫

 更新時間:2021年08月25日 15:02:56   作者:imJackXu  
這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)貝塞爾曲線動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了iOS實現(xiàn)貝塞爾曲線動畫的具體代碼,供大家參考,具體內(nèi)容如下

效果如圖:

仿美人相機(jī),手勢滑動隱藏頂部view。為了方便講解,將屏幕分為幾個區(qū)域,如圖:

在拖動過程中:

1、拖動距離小于minMoveDistance,貝賽爾曲線發(fā)生形變
2、拖動大于minMoveDistance,整個view開始下移

在松開手時:

1、拖動距離小于minMoveDistance,未發(fā)生位移,貝塞爾曲線恢復(fù)形變
2、拖動大于minMoveDistance,小于minDisappearDistance,貝塞爾曲線恢復(fù)形變、位移回到初始位置。
3、拖動大于minDisappearDistance,向下移動隱藏view

一、根據(jù)y軸位移量確定貝塞爾路徑

在拖動時曲線形變、松手時曲線恢復(fù)形變的時候都需要改變貝塞爾曲線,實際上是改變二階貝塞爾曲線的控制點,因此寫一個方法,根據(jù)傳入的y軸位移量得到新的貝塞爾路徑:

- (CGPathRef)getPathWithMoveDistance:(CGFloat)distance{

    UIBezierPath *path = [UIBezierPath bezierPath];
    CGPoint startPoint = CGPointMake(0, 0);
    CGPoint controlPoint = CGPointMake(self.bounds.size.width*0.5, 60+distance);
    CGPoint endPoint = CGPointMake(self.bounds.size.width, 0);

    [path moveToPoint:startPoint];
    [path addQuadCurveToPoint:endPoint controlPoint:controlPoint];

    [path addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height)];
    [path addLineToPoint:CGPointMake(0, self.bounds.size.height)];

    return path.CGPath;
}

二、初始化圖形

初始化的時候,可以根據(jù)上一步的方法,傳入位移量0,獲取貝塞爾路徑,并將獲得的路徑賦給CAShapeLayer:

- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.originY = frame.origin.y;
        [self setUpLayer];
        [self addGesure];
    }
    return self;
}

- (void)setUpLayer{
    self.topLineLayer = [CAShapeLayer layer];
    self.topLineLayer.fillColor = ColorForTheme.CGColor;
    self.topLineLayer.strokeColor = ColorForTheme.CGColor;
    self.topLineLayer.path = [self getPathWithMoveDistance:0];
    [self.layer addSublayer:self.topLineLayer];
}

三、添加手勢

添加UIPanGestureRecognizer拖動手勢,并處理手勢拖動和結(jié)束拖動的事件。

拖動時:

1、拖動距離小于minMoveDistance,只有貝賽爾曲線發(fā)生形變,調(diào)用步奏一中的方法。
2、拖動距離大于minMoveDistance,整個view開始下移

結(jié)束拖動時:

調(diào)用revertFormY:方法,傳入當(dāng)前在y軸上已經(jīng)發(fā)生的位移量

- (void)addGesure{
    if (self.gesture == nil) {
        self.gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    }
    [self addGestureRecognizer:self.gesture];
}

- (void)handlePan:(UIPanGestureRecognizer *)gesture{
    CGFloat distanceX = [gesture translationInView:self].x;
    CGFloat distanceY = [gesture translationInView:self].y;

    if (ABS(distanceX) > ABS(distanceY)) {
        return;
    }
    //拖動過程
    if (gesture.state == UIGestureRecognizerStateChanged) {
        NSLog(@"%f",distanceY);

        //移動少于minMoveDistance,貝賽爾曲線形變
        if (distanceY > 0 && distanceY <= minMoveDistance) {
            self.topLineLayer.path = [self getPathWithMoveDistance:distanceY];
        }
        //移動大于minMoveDistance,整個view下移
        else if (distanceY > minMoveDistance) {
            self.frame = CGRectMake(0, self.originY+distanceY-minMoveDistance, self.bounds.size.width, self.bounds.size.height);
        }
    }
    //手勢結(jié)束
    if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled || gesture.state == UIGestureRecognizerStateFailed) {
        [self removeGestureRecognizer:self.gesture];
        [self revertFormY:distanceY];
    }
}

四、revertFormY:恢復(fù)形變方法

根據(jù)傳入的y軸上的位移量,實現(xiàn)不同的效果:

1、y小于minMoveDistance,未發(fā)生位移,貝塞爾曲線恢復(fù)形變
2、y大于minMoveDistance,小于minDisappearDistance,貝塞爾曲線恢復(fù)形變、位移回到初始位置。
3、y大于minDisappearDistance,向下移動隱藏view

//手勢結(jié)束后恢復(fù)或隱藏
-(void)revertFormY:(CGFloat)y{

    //y < 最小的隱藏位移距離,未發(fā)生位移,貝塞爾曲線恢復(fù)動畫
    if (y < minDisappearDistance) {
        CAKeyframeAnimation *vibrate = [CAKeyframeAnimation animationWithKeyPath:@"path"];
        vibrate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
        vibrate.values = @[
                           (id) [self getPathWithMoveDistance:y],
                           (id) [self getPathWithMoveDistance:-(y * 0.3)],
                           (id) [self getPathWithMoveDistance:(y * 0.2)],
                           (id) [self getPathWithMoveDistance:-(y * 0.15)],
                           (id) [self getPathWithMoveDistance:(y * 0.1)],
                           (id) [self getPathWithMoveDistance:-(y * 0.07)],
                           (id) [self getPathWithMoveDistance:(y * 0.05)],
                           (id) [self getPathWithMoveDistance:0.0]
                           ];
        vibrate.duration = 0.5;
        vibrate.removedOnCompletion = NO;
        vibrate.fillMode = kCAFillModeForwards;
        vibrate.delegate = self;
        [self.topLineLayer addAnimation:vibrate forKey:nil];
    }

    //y > 最小位移距離,發(fā)生了位移
    if(y > minMoveDistance){
        [UIView animateWithDuration:0.3 animations:^{
            CGFloat endY;
            //向上恢復(fù)view
            if (y < minDisappearDistance) {
                endY = self.originY;
            }
            //向下隱藏view
            else{
                endY = SCREEN_HEIGHT;
            }
            self.frame = CGRectMake(0, endY, SCREEN_WIDTH, self.frame.size.height);
        }];
    }
}

五、雙擊屏幕空白,消失的view從底部上移,恢復(fù)到初始位置

這個只是為了回到初始狀態(tài),根據(jù)實際需求實現(xiàn)。

//恢復(fù)到初始位置
- (void)comeBack{
    if (self.frame.origin.y <= self.originY) {
        return;
    }

    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, self.originY, SCREEN_WIDTH, self.frame.size.height);
    } completion:^(BOOL finished) {
        [self revertFormY:10];
    }];
}

完整代碼

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論