简体   繁体   English

CABasicAnimation CGRect放大无法正常工作

[英]CABasicAnimation CGRect enlarging not working

I am trying to use a CABasicAnimation in order to make a view larger and then smaller, using the autoreverse. 我正在尝试使用CABasicAnimation,以便使用自动反转使视图变大然后变小。 I have QuartzCore imported. 我已经导入了QuartzCore。 I am using the code below: 我正在使用以下代码:

    CABasicAnimation *enlarge = [CABasicAnimation animationWithKeyPath:@"enlargeKeyPath"];
    CGRect currentSize = CGRectMake(20, 27, _moveView.bounds.size.width, _moveView.bounds.size.height);
    CGRect newSize = CGRectMake(20, 27, 45, 46);
    enlarge.fromValue = [NSValue valueWithCGRect:currentSize];
    enlarge.toValue = [NSValue valueWithCGRect:newSize];
    enlarge.autoreverses = YES;
    enlarge.repeatCount = 3.0;
    enlarge.duration = 0.5;
    [_moveView.layer addAnimation:enlarge forKey:@"enlargeKeyPath"];

However, that code does not work. 但是,该代码不起作用。 Could someone help? 有人可以帮忙吗?

I feel like something is wrong with the keys for the animationWithKeyPath at the beginning and the forKey and the end. 我觉得animationWithKeyPath的开头和forKey和结尾的键有问题。 I don't know what those are used for. 我不知道这些是用来干什么的。 Could somebody help? 有人可以帮忙吗? Thanks! 谢谢!

Instead of "enlargeKeyPath", what we need here is actually transform.scale ; 代替“ enlargeKeyPath”,我们实际上需要的是transform.scale ; as a result, we would animate the scaling as follows: 结果,我们将对缩放进行动画处理,如下所示:

CABasicAnimation *enlarge = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
enlarge.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(3.0f, 3.0f, 1.0f)];
//To value is the value we wish your view to be scaled up (or down) to.
enlarge.autoreverses = YES;
enlarge.repeatCount = 3.0;
enlarge.duration = 0.5;
[_moveView.layer addAnimation:enlarge forKey:@"enlargeKeyPath"];

If you really would like to scale on the frame, we should use the keyPath bounds instead of frame as mentioned. 如果您真的想在框架上缩放,则应使用keyPath bounds而不是如上所述的frame

Thus, your code would be: 因此,您的代码将是:

CABasicAnimation *enlarge = [CABasicAnimation animationWithKeyPath:@"bounds"];
CGRect currentSize = CGRectMake(20, 27, _moveView.bounds.size.width, _moveView.bounds.size.height);
CGRect newSize = CGRectMake(20, 27, 45, 46);
enlarge.toValue = [NSValue valueWithCGRect:newSize];
enlarge.autoreverses = YES;
enlarge.repeatCount = 3.0;
enlarge.duration = 0.5;
[_moveView.layer addAnimation:enlarge forKey:@"enlargeKeyPath"];

For other values of keyPath that you could use, please refer to the documentation here . 有关您可以使用的keyPath的其他值,请参阅此处的文档。

The following works for me: 以下对我有用:

CABasicAnimation *enlarge = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
CGSize currentSize = _moveView.bounds.size;
CGSize newSize = CGSizeMake(_moveView.bounds.size.height * 1.5, _moveView.bounds.size.width * 1.5);
enlarge.fromValue = [NSValue valueWithCGSize:currentSize];
enlarge.toValue = [NSValue valueWithCGSize:newSize];
enlarge.autoreverses = YES;
enlarge.repeatCount = INFINITY;
enlarge.duration = 5.0;
[_moveView.layer addAnimation:enlarge forKey:@"enlargeKeyPath"];

The basic problem you're encountering is confusing the animated key path (the parameter to animationWithKeyPath: ) and the animation key (the parameter to addAnimation:forKey: 您遇到的基本问题是将动画关键点路径( animationWithKeyPath:的参数)和动画关键点( addAnimation:forKey:的参数) addAnimation:forKey:

animationWithKeyPath: takes as a parameter the key path of an attribute of the view which will be animated. animationWithKeyPath:将要设置动画的视图属性的关键路径作为参数。 To animate the bounds of a view you can use "bounds", likewise you could use "bounds.origin" or "bounds.size" 要为视图的边界设置动画,可以使用“ bounds”,同样可以使用“ bounds.origin”或“ bounds.size”

addAnimation:forKey: takes as a parameter a key which allows you to identify this particular animation in the future. addAnimation:forKey:使用一个键作为参数,该键可让您将来识别此特定动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM