Đoạn mã sau sẽ xoay chế độ xem được gọi là bằng cách sử dụng hoạt ảnh được đóng khung. Hoạt ảnh bao gồm 3 khung hình trải dài trên 1 giây, với chế độ xem được xoay lần lượt là 0º, 180º và 360º ở khung hình đầu tiên, thứ hai và khung hình cuối cùng. Mã theo sau:
CALayer* layer = someView.layer;
CAKeyframeAnimation* animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.duration = 1.0;
animation.cumulative = YES;
animation.repeatCount = 1;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0 * M_PI],
[NSNumber numberWithFloat:0.5 * M_PI],
[NSNumber numberWithFloat:1.0 * M_PI], nil];
animation.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.5],
[NSNumber numberWithFloat:1.0], nil];
animation.timingFunctions = [NSArray arrayWithObjects:
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], nil];
[layer addAnimation:animation forKey:@"transform.rotation.z"];
Nếu sau hoạt động ngược chiều kim đồng hồ, bạn nên sử dụng giá trị âm. Để có hoạt ảnh cơ bản hơn một chút, bạn có thể sử dụng CABasicAnimation:
CALayer* layer = someView.layer;
CABasicAnimation* animation;
animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0 * M_PI];
animation.toValue = [NSNumber numberWithFloat:1.0 * M_PI];
animation.duration = 1.0;
animation.cumulative = YES;
animation.repeatCount = 1;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[layer addAnimation:rotationAnimation forKey:@"transform.rotation.z"];
Nguồn
2009-07-19 13:47:54
Tôi có phải bao gồm bất kỳ điều gì đặc biệt không? Xcode phàn nàn về tất cả những thứ kCA như kCAMediaTimingFunctionLinear. Họ không rõ. – Thanks
".objc_class_name_CAKeyframeAnimation", tham chiếu từ: đen-con trỏ @__ ObjC @__ cls_refs @ CAKeyframeAnimation trong TestClass.o ld: biểu tượng (s) không tìm thấy collect2: ld trở về tình trạng 1 lối – Thanks
Bất cứ khi nào bạn có một vấn đề với một cái gì đó không được tìm thấy, bạn nên tìm kiếm nó trong tài liệu để xem nơi nó được xác định. Trong trường hợp này, các hằng số này được định nghĩa trong CAMediaTiming & CAMediaTimingFunction là một phần của khung công tác QuartzCore. Nếu bạn chưa thêm QuartzCore.framework, đó có thể là vấn đề của bạn. –