animator.startAnimation--这个警告是什么意思



我正在学习如何在objc中使用UIViewPropertyAnimator。我用一个名为"blueBox"的对象制作了一个简单的测试应用程序。我想改变blueBox的属性。

我在@implementation之外声明"animator"@结束:

UIViewPropertyAnimator *animator;

然后这样定义:

- (void)viewDidLoad {
[super viewDidLoad];
CGRect newFrame = CGRectMake(150.0, 350.0, 100.0, 150.0);
animator = [[UIViewPropertyAnimator alloc]
initWithDuration:2.0
curve:UIViewAnimationCurveLinear
animations:^(void){
self.blueBox.frame = newFrame;
self.blueBox.backgroundColor = [UIColor redColor];
}];
}

当我想使用它时,我会写:

animator.startAnimation;

它按预期工作(更改对象的颜色和帧(,但"animator.startAnimation;"上有警告上面写着"属性访问结果未使用-getter不应用于副作用"。这指的是什么属性访问结果?我该怎么写才不会收到警告?

startAnimation是一个方法,而不是属性。你应该写:

[animator startAnimation];

虽然Objective-C确实允许您在调用不带参数的方法时使用属性语法,但您的使用就像您试图读取属性值一样。但是,由于(显然(您没有尝试存储结果(没有(,编译器会抱怨您忽略了访问的值。

只要避免错误的语法,就可以避免问题。

顺便说一句,你声称线路:

UIViewPropertyAnimator *animator;

@implementation/@end对之外。这使它成为一个文件全局变量。这是你真正想要的吗?如果您希望它是类的实例变量(这可能是您真正想要的(,那么它应该是:

@implementation YourClass {
UIViewPropertyAnimator *animator; //instance variable
}
// your methods
@end

最新更新