간단한 커스텀 뷰에 액션을 추가하는 간단한 작업을 해왔습니다. 나는 인터넷을 통해 보면서이 "쉬운"해결책을 발견 :커스텀 뷰에 액션 추가하기
UITapGestureRecognizer
UIButton
나는 프로그램이 작업을 수행 할 수 난 그냥 수돗물을 처리해야합니다.
내 코드는 여기까지입니다. 두 솔루션을 따로 따로 사용해 보았는데 작동하지 않습니다!
하는 .m
#import "AreaView.h"
@implementation AreaView
#define GREY 27.0/255.0
#define PINK_R 252.0/255.0
#define PINK_G 47.0/255.0
#define PINK_B 99.0/255.0
- (id) initWithFrame:(CGRect)frame imageName:(NSString *)imageName areaName:(NSString *)areaName minimumSpending:(int)minimumSpending andCapacity:(int)capacity
{
self = [self initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithRed:GREY green:GREY blue:GREY alpha:1];
self.userInteractionEnabled=YES;
//Init variables
_areaName=areaName;
_capacity=capacity;
_minimumSpending=minimumSpending;
//Image view
_logoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 4, 66, 50)];
//_logoImageView.image = [UIImage imageNamed:imageName];
_logoImageView.backgroundColor = [UIColor grayColor];
//Label
_areaNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 54, 76, 18)];
_areaNameLabel.textAlignment = NSTextAlignmentCenter;
_areaNameLabel.textColor = [UIColor whiteColor];
_areaNameLabel.font = [UIFont systemFontOfSize:12.0];
_areaNameLabel.text = areaName;
//button
_button = [[UIButton alloc]initWithFrame:self.bounds];
_button.userInteractionEnabled=YES;
_button.backgroundColor=[UIColor yellowColor];
[_button addTarget:self action:@selector(handleTap:) forControlEvents:UIControlEventTouchUpInside];
//tap gesture racognizer
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[self addGestureRecognizer:tapRecognizer];
[self addSubview:_logoImageView];
[self addSubview:_areaNameLabel];
[self addSubview:_button];
}
return self;
}
-(void)handleTap:(UIButton *)button
{
NSLog(@"tapped!");
}
-(void)tapped:(UITapGestureRecognizer *)recognizer
{
NSLog(@"tapped!");
}
@end
.H 도와
#import <UIKit/UIKit.h>
@interface AreaView : UIView <UIGestureRecognizerDelegate>
@property (nonatomic) UIImageView *logoImageView;
@property (nonatomic) UILabel *areaNameLabel;
@property (nonatomic) NSString *areaName;
@property (nonatomic) int minimumSpending;
@property (nonatomic) int capacity;
- (id) initWithFrame:(CGRect)frame imageName:(NSString *)imageName areaName:(NSString *)areaName minimumSpending:(int)minimumSpending andCapacity:(int)capacity;
@property (nonatomic, strong) UIButton *button;
@end
감사합니다!
문제
편집 handleTap하고 탭 모두 내가 버튼 솔루션 또는 개별적으로 테스트 할 수있는 탭 제스처를 언급하는 경우에도 해고되지 않습니다 것입니다. 버튼 구현의 경우 인터페이스에서 볼 수 있지만 아무 것도 클릭하지 않습니다.
내 UIView는 UIScrollview에서 여러 번 (여러보기의 경우) 프로그래밍 방식으로 추가됩니다.
편집 2
문제는 그 이상 복잡하다. 커스텀 뷰는 히트 테스트를 재 작성하는 주된 기능을 가진 또 다른 커스텀 뷰 안에있는 스크롤 뷰 내부에 있으며,이 뷰의 터치는 스크롤 뷰에 의해 유지됩니다. (Here is the purpose of all that).
hittest가 관련되어있는 한 작동하지 않습니다.
'작동하지 않는다'는 것은 무엇을 의미합니까? 버튼 액션이 실행됩니까? – Mani
어디에서 붙였습니까? –
적어도 하나의 이벤트가 있어야한다고 생각합니다. 코드를 다시 확인하십시오. – Chathurka