我想要做的是以編程方式在屏幕左上角創建一個UIView矩形,然後將其移到右上角,右下角,左下角,最後回到左上角。但它不能按預期工作。我的代碼有什麼問題?爲什麼UIView動畫不起作用
#import "ViewController.h"
@interface ViewController()
@property (nonatomic,strong) UIView *myView;
@end
@implementation ViewController
@synthesize myView = _myView;
- (IBAction)animation:(UIButton *)sender {
[UIView animateWithDuration:3.0 animations:^{
self.myView.alpha = 0.75;
self.myView.frame = CGRectMake(160, 0, 160,230);}];
[UIView animateWithDuration:3.0 animations:^{
self.myView.alpha = 0.50;
self.myView.frame = CGRectMake(160, 230, 160,230);}];
[UIView animateWithDuration:3.0 animations:^{
self.myView.alpha = 0.25;
self.myView.frame = CGRectMake(0, 230, 160,230);}];
[UIView animateWithDuration:3.0 animations:^{
self.myView.alpha = 0.00;
self.myView.frame = CGRectMake(0, 0, 160,230);}
completion:^(BOOL finished) {
[self.myView removeFromSuperview];
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewRect = CGRectMake(0, 0, 160, 230);
UIView *mv = [[UIView alloc] initWithFrame:viewRect];
self.myView = mv;
self.myView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.myView];
}
@end
編輯: 我修復嵌套完成塊的問題:
- (IBAction)animation:(UIButton *)sender {
[UIView animateWithDuration:3.0 animations:^{
self.myView.alpha = 0.75;
self.myView.frame = CGRectMake(160, 0, 160,230);}
completion:^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
self.myView.alpha = 0.50;
self.myView.frame = CGRectMake(160, 230, 160,230);}
completion:^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
self.myView.alpha = 0.25;
self.myView.frame = CGRectMake(0, 230, 160,230);}
completion:^(BOOL finished) {
[UIView animateWithDuration:3.0 animations:^{
self.myView.alpha = 0.00;
self.myView.frame = CGRectMake(0, 0, 160,230);}
completion:^(BOOL finished) {
[self.myView removeFromSuperview];
}];}];}];}];}
但它是可怕的閱讀。有沒有其他方法?
嘗試_after_當前部分是啓動動畫的下一部分完成,不與它平行。可憐的'UIView'不能決定它應該做什麼。 :) – holex
聽起來有趣..你能更具體,並把代碼在答案? – Philip007