开发者

How to ignore touches after a view transition?

I have two views, the main view where I am using CALayers to do animation. This main layer handles touch events touchesBegan, touchesMoved and touchesEnded.

The second view is an information view that has it's own nib and contains the app instructions. When the user double taps a button in the main view I flip the information view into place with...

if (infoView == nil) {
    NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"InfoView" owner:self options:nil];
    infoView = [ nibViews objectAtIndex: 0];
    [infoView retain];
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[self view] cache:YES];
[[self view] addSubview:infoView];
[UIView commitAnimations];

When the user taps a button in the information view I flip the main view back into place with...

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[self view] cache:YES];
    [infoView removeFromSuperview];
    [UIView commitAnimations];

The problem I am having is that, when I'm in the information view, touches are still being received by the main view (which was flipped out of view). Buttons and text fields in the information view are responding to touches properly.

So the question is, how can I disable the main view from receiving touch events when the information view has been flipped in开发者_运维知识库to place?

UPDATE: It appears I have solved my problem, but in the process have created a new problem. What I am try to do is manage two UIVIews with a single UIViewController. The problem I had was that the main view controller loaded the main view and when I switched views I added the info view as a subview of main view. This hierarchy apparently caused touches that were not used in the visible info view to be passed down to the main view.

I solved this problem by having the main view controller load a container view. The main view was then loaded as a subview of the container view. When I flipped in the info view I added it as a subview of the container view. The unused touches that occur in the info view are now passed to the container view and the main view never sees them.

But, I have created a new problem. When the main view is visible it only receives touchdown events in the upper portion of the view. I can touchdown in the upper portion of the view and drag into the lower portion and receive the touchmoved and touchended events. I just don't get the touchdown events in the lower portion.

Any help would be appreciated.


Consider presenting your 2nd view using presentModalViewController:animated: instead. You can get the flip effect, and you won't have a problem with touches, because the new view controller will be the first responder, and handle all touches automagically.

e.g.

MyInfoViewController* infoView = [[MyInfoViewController alloc] initWithNibName:@"MyInfoViewController" bundle:nil];
infoView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:infoView animated:YES];

You can easily create this new view controller and nib file using xcode's new file command (make a UIViewController subclass, and check "use nib")

You dismiss the view controller by calling [self dismissModalViewController]; from your main view controller.

You will have to set up some type of notification or delegate protocol between the two view controllers in order to let your info view controller notify your main view controller when the user wishes to dismiss it.

You can look at the template project for the Utility type application in xcode for an example of this exact thing.


I think you can inspect your view hierarchy while receiving touch events and then evaluate if it's good to receive this touch or just discard it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜