1

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

我在@implementation 之外声明'animator' ... @end:

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;' 上有警告 上面写着“未使用的属性访问结果 - 不应将吸气剂用于副作用”。指的是什么属性访问结果?我应该怎么写这样我就不会收到警告?

4

1 回答 1

2

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

[animator startAnimation];

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

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

顺便说一句,您声称该行:

UIViewPropertyAnimator *animator;

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

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

// your methods

@end
于 2018-12-15T03:13:01.247 回答