2/20/2014

[iOS開發] UIImageView 加點擊範例

簡單的讓 UIImageView 變成 ImageButton imgBtn.png 放在 Resources 資料夾
//生成 UIImage 並指定圖片
UIImage *image = [UIImage imageNamed:@"imgBtn.png"];
UIImageView *iv = [[UIImageView alloc] initWithImage:image];
//指定位置 x,y,width,height
iv.frame = CGRectMake(10, 10, image.size.width, image.size.height);

//讓 UIImage 能接受使用者事件
iv.userInteractionEnabled = YES;

//UITapGestureRecognizer 是觸控控制類
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[iv addGestureRecognizer:singleTap];

[self.view addSubview:iv];

-(void)tapDetected{
  NSLog(@"Click!!");
}

沒有留言:

張貼留言