IOS UIView直接響應點擊事件的解決方法

UIView的手勢處理能夠響應點擊事件。(此處使用 UIImageView 做爲示例)ide

  1.  self.allScreenImg.userInteractionEnabled = YES;//UIImageView默認不響應手勢,須要開啓userInteractionEnabledspa

    //處理單擊事件.net

    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAllScreen:)];orm

    [self.allScreenImg addGestureRecognizer:singleTap];事件


2. 添加處理的方法get

- (void) clickAllScreen:(UITapGestureRecognizer *) recognizer {it

    AllScreenViewController* recordViewController = [[AllScreenViewController alloc] initWithStoryboard];io

    [self.navigationController pushViewController:recordViewController animated:YES];event

}class


以上操做便可以響應了UIImageView的點擊事件了。

方法二:

爲UIImageView設置tag,直接處理手勢 以處理單擊事件:

    p_w_picpathView.setTag = 333;

   -(void)touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event {

        UIImageView* img = (UIImageView *) [self.view viewWithTag: 333];

        if (img) {

            // TODO some thing

        }

    }




 參考博文:  http://www.tekuba.net/program/274/

相關文章
相關標籤/搜索