2015-09-08 2 views
2

Я создал пользовательский класс представления. Я инициализирую его в классе контроллера вида. Я включил взаимодействие с пользователем, а затем он не работает. Я искал его в похожих вопросах, но большинство из них говорит, чтобы включить взаимодействие с пользователем.
Вот код, который я написал.UITapGestureRecognizer не работает над пользовательским классом UIView

@interface ProfileCreatorViewController(){ 

SectionTitleView *ProfileTitle; 
CGRect mainFrame; 

} 

@end 

@implementation ProfileCreatorViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    mainFrame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + NAVBAR_HEIGHT, self.view.frame.size.width, self.view.frame.size.height); 

    CGRect profileFrame = CGRectMake(mainFrame.origin.x + 5, mainFrame.origin.y, mainFrame.size.width - 20, 50); 
    ProfileTitle = [[SectionTitleView alloc]initWithFrame:profileFrame withTitle:@"Profile" withUnderLineColor:[UIColor blackColor] withDownButton:[UIImage imageNamed:@"rightArrow"]]; 
    [self.view addSubview:ProfileTitle]; 

    UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(downButtonClicked)]; 
    [ProfileTitle addGestureRecognizer:recog]; 
    ProfileTitle.userInteractionEnabled = YES; 

} 


-(void) downButtonClicked{ 

    NSLog(@"clicked"); 
} 

ответ

2
@interface ProfileCreatorViewController()<UIGestureRecognizerDelegate>{ 

SectionTitleView *ProfileTitle; 
CGRect mainFrame; 

} 

@end 

@implementation ProfileCreatorViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    mainFrame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + NAVBAR_HEIGHT, self.view.frame.size.width, self.view.frame.size.height); 

    CGRect profileFrame = CGRectMake(mainFrame.origin.x + 5, mainFrame.origin.y, mainFrame.size.width - 20, 0); 
    ProfileTitle = [[SectionTitleView alloc]initWithFrame:profileFrame withTitle:@"Profile" withUnderLineColor:[UIColor blackColor] withDownButton:[UIImage imageNamed:@"rightArrow"]]; 
    [self.view addSubview:ProfileTitle]; 

    UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(downButtonClicked:)]; 
    recog.delegate=self; 
    [ProfileTitle addGestureRecognizer:recog]; 
    ProfileTitle.userInteractionEnabled = YES; 

} 


-(void) downButtonClicked:(UITapGestureRecognizer *)sender{ 

    NSLog(@"clicked"); 
} 

Добавить делегат и изменить свой метод подписи

+0

Нет, до сих пор не WOKING. Спасибо хоть. –

+0

'NSLog' youe view проверить, присвоено ли звание тегу или нет – Mukesh

2

Вы можете проверить несколько вещей здесь высота и ширина profileFrame являются не очень мало (печать profileFrame)

ProfileTitle не является полным прозрачный (Жест также не работает, если альфа-представление очень близко к 0)

Профиль ProfileTitle не закрыт никаким другим вид (использовать визуальный отладчик для этого)

2

Попробуйте эти ..

создать свойство пользовательского зрения как these..and также импортировать его в файл ProfileCreatorViewController.h как эти ..

#import "SectionTitleView.h" 

@property (nonatomic , strong) SectionTitleView *previewView; 

Добавить UIGestureRecognizerDelegate

@interface ProfileCreatorViewController()<UIGestureRecognizerDelegate> 
{ 
    SectionTitleView *ProfileTitle; 
    CGRect mainFrame; 
} 

набор NumberOfTapsRequired, если вы хотите

UITapGestureRecognizer *recog = 
    [[UITapGestureRecognizer alloc]initWithTarget:self 
             action:@selector(downButtonClicked:)];  
ProfileTitle.userInteractionEnabled = YES; 
[recog setNumberOfTapsRequired:1]; //No. of taps.. 
[ProfileTitle addGestureRecognizer:recog]; 

, а также способ его

-(void) downButtonClicked:(UITapGestureRecognizer*)gesture 
{ 
    NSLog(@"Taped"); 
} 

я надеюсь, что это помогает ..

0

Вам нужен UIGestureRecognizerDelegate в файле заголовка. создать Также property держать UITapGestureRecognizer внутри:

@property (strong, nonatomic) UITapGestureRecognizer tapGesture; 

Вы также не должны создавать UITapGestureRecognizer внутри родительского класса. ProfileTitle должен генерировать это сам по себе. Поэтому вам лучше вставить код внутри viewDidLoad внутри SectionTitleView.

FYI: не используют капитализированные имена переменных

Смежные вопросы