用OC写
时间: 2023-08-19 09:05:48 浏览: 102
windows上用OC
好的,以下是一个用 Objective-C 写的 UIImageView 移动动画示例代码:
```objective-c
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"image"];
[self.view addSubview:imageView];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
animation.duration = 1.0;
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
[imageView.layer addAnimation:animation forKey:@"positionAnimation"];
```
该代码与 Swift 版本的代码相似,只是语法不同。需要注意的是,在 Objective-C 中需要使用 `@property` 语法或 `setValue:forKey:` 方法来设置 CABasicAnimation 的属性。此外,在 Objective-C 中需要使用 `kCAFillModeForwards` 来表示 fillMode 的属性值。
阅读全文