제스처 인식기로 터치 이벤트를 가로 채고 프로그래밍 방식으로 모든 uibutton에 인식기를 추가 할 수 있습니다. 예를 들면 : 당신이 당신의 버튼에 제스처 인식기를 추가 할 것
//
// HighlighterGestureRecognizer.h
// Copyright 2011 PathwaySP. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HighlightGestureRecognizer : UIGestureRecognizer {
id *beganButton;
}
@property(nonatomic, assign) id *beganButton;
@end
and the implementation:
//
// HighlightGestureRecognizer.m
// Copyright 2011 PathwaySP. All rights reserved.
//
#import "HighlightGestureRecognizer.h"
@implementation HighlightGestureRecognizer
@synthesize beganButton;
-(id) init{
if (self = [super init])
{
self.cancelsTouchesInView = NO;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.beganButton = [[[event allTouches] anyObject] view];
if ([beganButton isKindOfClass: [UIButton class]]) {
[beganButton setBackgroundImage:[UIImage imageNamed:@"grey_screen"] forState:UIControlStateNormal];
[self performSelector:@selector(resetImage) withObject:nil afterDelay:0.2];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)reset
{
}
- (void)ignoreTouch:(UITouch *)touch forEvent:(UIEvent *)event
{
}
- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer
{
return NO;
}
- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer
{
return NO;
}
- (void)resetImage
{
[beganButton setBackgroundImage: nil forState:UIControlStateNormal];
}
@end
방법은과 같이 될 것이다 :
HighlighterGestureRecognizer * tapHighlighter = [[HighlighterGestureRecognizer alloc] init];
[myButton addGestureRecognizer:tapHighlighter];
[tapHighlighter release];
을 그러니까 기본적으로 당신이 그것을 선언을 초기화 한 다음를 추가하고 있습니다. 그 후에, addGestureRecognizer가 그것을 유지하기 때문에, 그것을 해제하고 싶을 것이다.
또한 단순히 버튼
adjustsImageWhenHighlighted = YES
세트를 시도? 기본값은 YES이지만 아마도 xib에서 변경했을 것입니다. 속성 관리자의 "강조 표시된 이미지 조정"체크 박스 :