搜尋

首頁  >  問答  >  主體

objective-c - ios 使用約束版面後動畫失敗

運行這段程式碼後,動畫效果失敗

#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@property (nonatomic,strong)UIView *a;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIView *a = [[UIView alloc] init];
    a.backgroundColor = [UIColor redColor];
    [self.view addSubview:a];
    self.a = a;
    
    [a mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom);
        make.height.equalTo(@100);
    }];
    
    
    UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 200 , 200)];
    b.backgroundColor = [UIColor greenColor];
    [self.view addSubview:b];
    [b addTarget:self action:@selector(ad) forControlEvents:UIControlEventTouchUpInside];
}

-(void)ad{
    [UIView animateWithDuration:2 delay:1 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.a.center = CGPointMake(self.a.center.x, self.a.center.y - 200);
    } completion:^(BOOL finished) {
        NSLog(@"11111 %@",self.a);

        [self.view addSubview:[[UIView alloc] init]];
          NSLog(@"12222222 %@",self.a);
    }];
}

點擊按鈕,動畫效果會在最後恢復到最初始的位置,是否有人能夠簡單解答一下,為什麼[self.view addSubview:[[UIView alloc] init]]; 會造成這個問題

为情所困为情所困2786 天前526

全部回覆(1)我來回復

  • 黄舟

    黄舟2017-05-02 09:36:40

    當你用autolayout佈局後,對應的view如果需要做動畫,也要改變佈局,而不是frame。

    回覆
    0
  • 取消回覆