私がやろうとしているのは、プログラムで画面の左上隅に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];
}];}];}];}];}
しかし、読むのはひどいです。他に方法はありますか?