开发者

Stall in animation

I am using this code from this site

http://ramin.firoozye.com/2009/09/29/semi-modal-transparent-dialogs-on-the-iphone/

to show a modal view and remove it. It displays fine ie drops in from the top but when being removed it stalls just on its way out just for a fraction of second but its noticeable how do i get rid of the stall.

The view i am showing is view of a viewcontroller which is a memeber of the parent viewcontroller so i call methods like this to display

[self showModal:self.modalController.view]

to hide

[self hideModal:self.modalController.view];

.

- (void) showModal:(UIView*) modalView
{
    UIWindow* mainWindow = (((SessionTalkAppDelegate*) [UIApplication sharedApplication].delegate).window);

    //CGPoint middleCenter = modalView.center;
    CGPoint middleCenter = CGPointMake(160, 205);
    CGSize offSize = [UIScreen mainScreen].bounds.size;
    //CGPoint offScreenCenter = CGPointMake(offSize.width / 2.0, offSize.height * 1.5);
    CGPoint offScreenCenter = CGPointMake(offSize.width / 2.0, -100); // start from top
    modalView.center = offScreenCenter; // we start off-screen
    [mainWindow addSubview:modalView];

    // Show it with a transition effect
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3]; // animation duration in seconds
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    modalView.center = middleCenter;
    [UIView commitAnimations];
}

// Use this to slide the semi-modal back up.
- (void) hideModal:(UIView*) modalView
{
    CGSize offSize = [UIScreen mainScreen].bounds.size;
    //CGPoint offScreenCenter = CGPointMake(offSize.width / 2.0, offSize.height * 1.5);
    CGPoint offScreenCenter = CGPointMake(offSize.width / 2.0, -100);
    [UIView beginAnimations:nil context:modalView];
    [UIView setAnimationDurat开发者_Python百科ion:0.3];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(hideModalEnded:finished:context:)];
    modalView.center = offScreenCenter;
    [UIView commitAnimations];
}

- (void) hideModalEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    UIView* modalView = (UIView *)context;
    [modalView removeFromSuperview];
    //[modalView release];
}


I think your problem arises from (1) this isn't actually a real modal view and (2) you seem to have two active view controllers in the responder chain.

(1) This isn't a true modal view. It is just an ordinary subview that is added as a subview of the main window. The difference is that a real modal view and its controller capture the entire responder chain until dismissed. All touches and events (like shakes or rotations) are handled by either the modal view or its controller. This is what the call to presentModalViewController: actually does. It redirects the responder chain.

(2) Right now you have two view controllers in the responder chain. The main view controller continues to function and to listen to the responder chain as the fake-modal view controller and view move about. Since the fake-modal is added as a subview of window below the main view controller and is in fact added by main view controller, the main view controller is always active and processing events.

The delay is most likely caused by main view controller responding to some event caused by the movement of the faux-modal view across the views of the main controller. As the views (all the UIElements) of the main controller are exposed, they redraw themselves which produces events. In a real modal view, the events are not sent up the chain to the main view until the modal view is gone. In this case, they are.

You can ensure a smooth animation by making the main controller the controller of the faux-modal view. That way, you end up with a normal view controller to view relationship without the responder chain snarl.

Also, this:

- (void) hideModalEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context

... is poor practice. Don't use a void pointer. Use id instead like so:

- (void) hideModalEnded:(NSString *)animationID finished:(NSNumber *)finished context:(id)context

... then cast it to a UIView. Since your passing a UIView subclass anyway you could just set it to a UIView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜