animateWithDuration:delay:options:animations:completion:not working(很奇怪)

时间:2013-12-09 10:40:07

标签: ios objective-c uiview core-animation

我正在尝试使用以下代码将UIView转换为距离:

CGAffineTransform transform = CGAffineTransformMakeTranslation(-100, -100);
[self.exposureHintView setTransform:transform];
[UIView animateWithDuration:2.0
                      delay:0.0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     NSLog(@"Begin!");
                     CGAffineTransform newTrans = CGAffineTransformMakeTranslation(0, 0);
                     [self.exposureHintView setTransform:newTrans];
                 }
                 completion:^(BOOL finished) {
                     NSLog(@"End %d", finished);
                 }];

所以基本上,我只是试图将视图从(-100, -100)转到相对于自身应该(0, 0)的位置。

因为我希望在视图出现后为其设置动画,所以我将代码放在viewDidAppear:中。但是当我运行代码时,什么也没发生。

self.exposureHintView这里是UIView的自定义子类,这有关系吗?

为什么?

2 个答案:

答案 0 :(得分:1)

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[_AboutView setFrame:CGRectMake(0, 0, 320, 510)];
[UIView commitAnimations];

使用此代码更改视图的位置

答案 1 :(得分:0)

我认为这里的问题是你在故事板或XIB文件中创建exposureHintView而没有代码编程。

尝试执行以下操作:

- (void)viewDidLoad
{
    //Init exposureHintView
    UIView* exposureHintView = [[UIView alloc]initWithFrame(sampleFrame)];
    [self addSubView: exposureHintView];

    //Set transform
    CGAffineTransform transform = CGAffineTransformMakeTranslation(-100, -100);
    [self.exposureHintView setTransform:transform];

    //Commit animation
    [UIView animateWithDuration:2.0 delay:0.0
                     options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
          NSLog(@"Begin!");
          CGAffineTransform newTrans = CGAffineTransformMakeTranslation(0, 0);
          [self.exposureHintView setTransform:newTrans];
    }
    completion:^(BOOL finished) {
        NSLog(@"End %d", finished);
    }];

这里的原因是当您使用storyboard或XIB时,您无法在>> ViewDidLoad中设置Frame或Transform或此UIView的其他属性