本文介绍了使用 Core Animation (CAKeyFrameAnimation) 调整大小和移动 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能吗?我可以更改图层的不透明度和位置(中心),但是每当我尝试更改大小或原点时,它都不起作用.

Is this possible? I can change the opacity and position (center) of the layer but whenever I try to change the size or origin, it doesn't work.

    CAAnimationGroup* anigroup = [CAAnimationGroup new];

    CGMutablePathRef thePath = CGPathCreateMutable();
    CGPathAddRect(thePath, NULL, CGRectMake(0,0, 320, 480));
    CGPathAddRect(thePath, NULL, CGRectMake(location.x - 16,location.y-24, 32, 48));
    CGPathAddRect(thePath, NULL, CGRectMake(190, 20, 32, 48));


    CAKeyframeAnimation* AniLoc = [CAKeyframeAnimation animationWithKeyPath:@"frame"];
    AniLoc.path = thePath;
    AniLoc.keyTimes= [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f],
                                             [NSNumber numberWithFloat:0.3f],
                                             [NSNumber numberWithFloat:1.0f],nil];
    AniLoc.duration = 5;

    CFRelease(thePath);

    anigroup.animations = [NSArray arrayWithObjects:AniLoc,nil];
    anigroup.duration = 5;

    [focusview.layer addAnimation:anigroup forKey:nil];

推荐答案

您可以在动画中更改视图或图层的大小和位置,但您需要作为一个动画组来这样做,而不是通过创建一系列矩形的路径.

You can both change the size and position of a view or layer in an animation, but you'll want to do so as an animation group, not by creating a path from a series of rectangles.

我在我的回答中提供了一个分组动画示例,该动画沿路径移动视图,同时缩小视图并降低其不透明度此处.这是通过对视图遵循的弯曲路径使用 CAKeyframeAnimation 来实现的(仅对其位置进行动画处理),然后使用基本动画来调整视图边界的大小.您也可以在那里使用 CAKeyframeAnimation,为您想要的每个大小的关键帧使用多个 CGSize,确保在将它们放入关键帧的 NSArray 之前将它们包装在 NSValue 中.

I provide an example of a grouped animation that moves a view along a path while shrinking it and lowering its opacity in my answer here. This works by using a CAKeyframeAnimation for the curved path that the view follows (only animating its position), and then using a basic animation to adjust the size of the view's bounds. You could also use a CAKeyframeAnimation there, with multiple CGSizes for each size keyframe you wanted, making sure to wrap them in an NSValue before placing them in an NSArray of keyframes.

为了同时为两个属性设置动画,我将这两个动画包装在一个 CAAnimationGroup 中并将其应用到视图层.

To animate two properties at once, I wrap the two animations in a CAAnimationGroup and apply that to the view's layer.

这篇关于使用 Core Animation (CAKeyFrameAnimation) 调整大小和移动 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:38