Thursday, November 21, 2013

iOS: Gotchas

Here's a couple of things that tripped me up today while developing an iOS app:

1. Views Blocking Touches

If you drag a plain view onto your view controller in storyboard, it will stop all touches reaching views located underneath. You will need to ensure the User Interaction Enabled setting is unchecked if you want your touches to be passed through.

2. Tap and Drag Location Tracking

Storyboard lets you easily wire up your buttons to methods in your view controller that get called whenever a user taps or drags inside the button. If you use Storyboard to create the stub method for you by dragging in the connection to an empty space in your view controller, it will create a method like:
- (IBAction)editClicked:(id)sender;
However, you may want to know where the user touched. Fortunately, this is easily available - just ensure you select the Sender and Event option in the Arguments drop-down when create the connection in Storyboard. This will then auto-create a method in your header file like:
- (IBAction)editClicked:(id)sender forEvent:(UIEvent *)event;
You can then get the touch position with the following code:
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint pointTouched = [touch locationInView:self.view];

No comments:

Post a Comment