简体   繁体   中英

UIView animate alpha when view is offscreen doesn't work

I have a UIView which is located offscreen and I'm animating the frame so that the view slides in offscreen from the bottom and is visible. I'd like to simultaneously animate the alpha property of a UILabel on the view as well so it fades in. Unfortunately it appears I can't do the alpha animation because the view is offscreen and doesn't appear to take hold. It looks something like this:

    nextCell.titleLabel.alpha = 0;
   [UIView animateWithDuration:collapsedAnimationDuration animations:^{
                        CGRect newFrame = lastCell.frame;
                        newFrame.origin = CGPointMake(lastCell.frame.origin.x , lastCell.frame.origin.y + THREAD_CELL_HEIGHT);
                        nextCell.frame = newFrame;
                        nextCell.titleLabel.alpha = 1;
                    }];

Is it not possible to start animating the alpha of the subview because it's offscreen? If I position the view on screen and then try the animation it looks great but that's not the effect I'm going for. Thanks for your help.

Is this code executed in cellForRowAtIndexPath ? If so, try moving it to tableView:willDisplayCell:forRowAtIndexPath: . The table view resets various properties of the cell before displaying it.

From the AppDelegate didFinishLaunching method:

self.myView = [[MyView alloc] initWithFrame:CGRectMake(320, 480, 400, 400)];
self.myView.titleLabel.text = @"test text";

self.myView.titleLabel.alpha = 0;
[UIView animateWithDuration:10.0 animations:^{
    CGRect newFrame = self.myView.frame;
    newFrame.origin = CGPointMake(0 , 0);
    self.myView.frame = newFrame;

    self.myView.titleLabel.alpha = 1;
}];

[self.viewController.view addSubview:self.myView];

MyView is just this:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self addSubview:self.titleLabel];
    }
    return self;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    }
    return _titleLabel;
}

I did no important changes to the code you presented and it worked fine. So assuming you're not doing what Tim mentioned (it won't work if you're doing it), we need more details to help you out.

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