Skip to content

Instantly share code, notes, and snippets.

@hatfinch
Created Jan 3, 2012
Embed
What would you like to do?
Empirical determination of TouchUpInside area
//
// TestView.m
// ButtonHitTest
//
// Created by Hamish Allan on 03/01/2012.
//
#import "TestView.h"
@implementation TestView
{
UIView *myArea;
UIButton *myButton;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
myArea = [[UIView alloc] init];
myArea.backgroundColor = [UIColor redColor];
myButton = [[UIButton alloc] init];
myButton.backgroundColor = [UIColor blueColor];
myButton.showsTouchWhenHighlighted = YES;
[myButton addTarget:self action:@selector(touchUpInside:forEvent:) forControlEvents:UIControlEventTouchUpInside];
[myButton addTarget:self action:@selector(touchUpOutside:forEvent:) forControlEvents:UIControlEventTouchUpOutside];
[myButton addTarget:self action:@selector(touchDragInside:forEvent:) forControlEvents:UIControlEventTouchDragInside];
[myButton addTarget:self action:@selector(touchDragOutside:forEvent:) forControlEvents:UIControlEventTouchDragOutside];
[self addSubview:myArea];
[self addSubview:myButton];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
myButton.bounds = CGRectMake(0.0, 0.0, 44.0, 44.0);
myButton.center = self.center;
CGRect touchUpArea = CGRectInset(myButton.bounds, -70.0, -70.0); // determined from NSLog output
myArea.frame = [myButton convertRect:touchUpArea toView:self];
}
- (void)touchUpInside:(id)sender forEvent:(UIEvent *)event
{
CGPoint location = [[[event touchesForView:myButton] anyObject] locationInView:myButton];
NSLog(@"touchUpInside: %@", NSStringFromCGPoint(location));
}
- (void)touchUpOutside:(id)sender forEvent:(UIEvent *)event
{
CGPoint location = [[[event touchesForView:myButton] anyObject] locationInView:myButton];
NSLog(@"touchUpOutside: %@", NSStringFromCGPoint(location));
}
- (void)touchDragInside:(id)sender forEvent:(UIEvent *)event
{
CGPoint location = [[[event touchesForView:myButton] anyObject] locationInView:myButton];
NSLog(@"touchDragInside: %@", NSStringFromCGPoint(location));
}
- (void)touchDragOutside:(id)sender forEvent:(UIEvent *)event
{
CGPoint location = [[[event touchesForView:myButton] anyObject] locationInView:myButton];
NSLog(@"touchDragOutside: %@", NSStringFromCGPoint(location));
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment