關(guān)鍵幀動畫

CALayer *transitionLayer = [[CALayer alloc] init];

 //開啟一個(gè)動畫事務(wù)
    [CATransaction begin];
    
    //CATransaction 事務(wù)類,可以對多個(gè)layer的屬性同時(shí)進(jìn)行修改.它分隱式事務(wù),和顯式事務(wù).kCATransactionDisableActions
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
    transitionLayer.opacity = 0.6;
    //contents是layer的一個(gè)屬性
    
    UIImageView *tempimgview;
    for (UIView *aView in [picView subviews]) // picView代表需要執(zhí)行動畫的視圖
    {
        if([aView isKindOfClass:[UIImageView class]]){
            //NSLog(@"=====找到imageview");
            tempimgview = (UIImageView*)aView;
            break;
        }
        
    }
//設(shè)置CALayer的內(nèi)容
transitionLayer.contents = (id)tempimgview.layer.contents;

//父類不同,所以需要坐標(biāo)系統(tǒng)的轉(zhuǎn)換,必須是處于同一window內(nèi)
    transitionLayer.frame = [[UIApplication sharedApplication].keyWindow convertRect:tempimgview.bounds fromView:tempimgview];
    
    [[UIApplication sharedApplication].keyWindow.layer addSublayer:transitionLayer];
   
    [UIView beginAnimations:@"imageViewAnimation" context:(__bridge void *)(tempimgview)];
/// 提交事務(wù)
    [CATransaction commit];

//路徑曲線:貝塞爾曲線,使動畫按照你所設(shè)定的貝塞爾曲線運(yùn)動
    UIBezierPath *movePath = [UIBezierPath bezierPath];
    [movePath moveToPoint:transitionLayer.position];
    
    //傳入購物車的坐標(biāo)(X,Y)
    CGPoint toPoint = CGPointMake(self.view.frame.size.width-20, 40);
    //
    [movePath addQuadCurveToPoint:toPoint
                     controlPoint:CGPointMake(self.view.frame.size.width-50,transitionLayer.position.y-50)];
    
    
    //關(guān)鍵幀
    CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    positionAnimation.path = movePath.CGPath;
    positionAnimation.removedOnCompletion = YES;
    
   
    
    //縮小動畫
    CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
    //縮小動畫結(jié)束移除
    scaleAnim.removedOnCompletion = YES;
    NSLog(@"=====in enjoy55555");
    
    //將拋物動畫和縮小動畫加入動畫組,可以執(zhí)行多個(gè)動畫,并且設(shè)置動畫的執(zhí)行時(shí)間
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.beginTime = CACurrentMediaTime();
    group.duration = 0.7;
    group.animations = [NSArray arrayWithObjects:positionAnimation,scaleAnim,nil];
    group.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    //group.fillMode = kCAFillModeForwards;
    group.removedOnCompletion = YES;
    group.autoreverses= NO;
    group.delegate=self;
    [transitionLayer addAnimation:group forKey:@"opacity"];
靜止界面
動畫開始
動畫執(zhí)行
動畫執(zhí)行
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容