有没有办法隐藏UIButton直到按下UIImageView? 当图片被按下时,我需要显示后退按钮,就像它在iPhone上的照片应用程序中工作一样? 这是我的UIButton的代码:
- (void)viewDidLoad {
[super viewDidLoad];
[self ladeImage];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 10, 40, 40);
[btn addTarget:self action:@selector(goToViewA) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:@"<<" forState:UIControlStateNormal];
[self.view addSubview:btn];
}
答案 0 :(得分:2)
第一步:btn.hidden = YES
然后你必须继承UIImageView以对其touchesEnded:
事件作出反应,并在那里更改按钮的隐藏属性。为此,正确的方法是创建一个协议(使用viewTouched方法)。在包含您的按钮和ImageView的viewController中实现该协议。将一个委托属性添加到子类ImageView(即id<MyCustomProtocol> _delagate;
)并将视图控制器分配给此属性。
答案 1 :(得分:0)
btn.hidden = YES;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image name"]];
imageView.userInteractionEnabled = YES; // here to enable touch event
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGestureRecongizer:)]; // handleTapGestureRecongizer is method will call when tap even fire
[imageView addGestureRecognizer:tap]; // Add Tap gesture recognizer to image view
[tap release], tap = nil;
[self.view addSubview:imageView];
[imageView release], imageView = nil;
方法handlerTapGestureRecognizer:
- (void)handleTapGestureRecongizer:(UITapGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
btn.hidden = NO;
}
}
玩得开心!