Created
January 3, 2012 01:01
-
-
Save hatfinch/1552915 to your computer and use it in GitHub Desktop.
Empirical determination of TouchUpInside area
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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