iPad Landscape trouble detecting touches on leftha

2019-09-03 03:52发布

问题:

I am working on a landscape iPhone app. It runs fine on the iPhone but when I run it on an iPad or the iPad simulator it has problems dealing with touches that exceed 320 in the x direction.

A lot of the UIButtons beyond 320 will highlight when pressed but won't do their action.

When I add this to my superviews:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
 UITouch *touch = [touches anyObject];

 CGPoint touchLocation = [touch locationInView:self];
 NSLog(@"bounds: %1.2f %1.2f  touch: %1.2f %1.2f",self.bounds.size.width,self.bounds.size.height,touchLocation.x,touchLocation.y);     
}

it will never output an x value greater than 320. Any part of the screen pressed that is greater than 320 gives an output of 320 for x.

The UIViews are all done programatically and without UIViewControllers. I have set the plist to start it in landscape and in the applicationDidFinishLaunching I rotate the views with this:

window.transform = CGAffineTransformMakeRotation((M_PI / 2.0));
window.bounds = CGRectMake(0, 0, 480, 320); 
window.center = CGPointMake(160.0f, 240.0f);
[window makeKeyAndVisible];

Any help is appreciated.

回答1:

I had a similar problem but I create all programmatically Window, View and ViewController (because it was a game using OpenGL ES).

The problem was that I originally created the window with the same rect as the view. To solve it just make the window in this way:

// Always create the window in Portrait mode
mpWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

and the viewController (who creates the View):

mpController = [[OViewController alloc] initWithFrame: CGRectMake(mPosX, mPosY, mWidth, mHeight)];
// After this line the view will rotate to landscape
[mpWindow addSubview:mpController.view];

Hope this helps!



回答2:

One of your parent views or the window has not the correct frame.

Since touches are evaluated from window upwards to your view it will stop if the touch is outside of any of your views parents.

you could debug it with code like:

UIView *parent = self ;
while (parent = [parent superview]) NSLog("parent: %@",NSStringFromCGRect(parent.frame));

this will print the frames in your frame hierachy ... check if one has a width of 320 pixel