简体   繁体   中英

Moving a UIView

I am trying to move a UIView programmatically using the following method:

-(void)animatePlaybackLine: (int) currentBeat{
    CGRect newFrame = CGRectMake(currentBeat*eighthNoteWidth, 0, eighthNoteWidth, 320);
    [playbackLine setFrame:newFrame];
    NSLog(@"Line animated to %i", currentBeat);
    NSLog(@"New frame origin %f", playbackLine.frame.origin.x);
}

When the method is called, the NSLogs show that the variable currentBeat is incrementing as it should, and the frame of playbackLine (my UIView) appears to be moving as it should. However, the object on the screen doesn't move. I have also tried setting the bounds and the center instead of the frame, and all of them have similar results. I also tried using an animation instead of setting the frame, to no avail.

Is there something else I should be doing to make the image onscreen show the changing frame?

I'm guessing you're calling that method in a loop? iOS isn't going to redraw the screen until your code finishes executing. You can't loop like this and see iterative results.

To animate views, you should use the native support UIKit gives you. In particular, check out the Animations section in the UIView class reference .

Try this

    [UIView animateWithDuration:0.3
                      delay:0.0
                    options: UIViewAnimationCurveEaseOut
                 animations:^{
                     datePickerView.frame = imageFrame;
                 } 
                 completion:^(BOOL finished){
                     self.datePickerViewFlag = NO;
                 }];

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