简体   繁体   中英

animateWithDuration:delay:options:animations:completion: not working (strangely)

I'm trying to translate a UIView for a distance using the following code:

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);
                 }];

So basically, I'm justing trying to move the view from some point say (-100, -100) to where it should be (0, 0) relative to itself.

Since I wanted to animate it once the view appeared, so I put the code in viewDidAppear: . But when I run the code, nothing happened.

The self.exposureHintView here is a custom subclass of UIView , does it matter?

Why?

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

Use this code to change the position of your view

I think the problem here is you are create exposureHintView in storyboard or XIB file without code programmatically.

Try to do as the follows:

- (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);
    }];

The reason here is your could not set Frame or Transform or some another attribute of this UIView in >>ViewDidLoad when you using storyboard or XIB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM