2012-10-13 6 views
2

탭 제스처 인식기를 설정하고 인식기를 uibutton에 추가했습니다. 버튼에는 배경 이미지가 있습니다. 버튼을 탭하면 전혀 강조 표시되지 않습니다. 내가 할 수 있었던 것은 알파 값을 변경하는 것뿐입니다.TapGestureRecognizer로 강조 표시 할 UIButton 가져 오기

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; 
    singleTap.cancelsTouchesInView = NO; 

    [btnNext addGestureRecognizer:singleTap]; 


- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture 
{ 
UIView *tappedView = [gesture.view hitTest:[gesture locationInView:gesture.view] withEvent:nil]; 
NSLog(@"Touch event view: %@",[tappedView class]); 
UIButton *myButton = (UIButton *) tappedView; 
[self highlightButton:myButton]; 
tappedView.alpha = 0.5f; 

} 

아무도 인정됩니다. 감사합니다.

답변

2

제스처 인식기로 터치 이벤트를 가로 채고 프로그래밍 방식으로 모든 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에서 변경했을 것입니다. 속성 관리자의 "강조 표시된 이미지 조정"체크 박스 :

enter image description here

관련 문제