[英]Tapping the Background image on iPhone
I want to do something when the background was tapped on iPhone. 在iPhone上点击背景时,我想做些事情。
How to enable the background tapping? 如何启用后台点击?
I put a sample background on my app using this code. 我使用此代码在我的应用程序上放置了示例背景。
- void viewDidLoad{
[super viewDidLoad];
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"backgroundimage.jpeg"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
How to do background tapping? 如何进行背景攻牙?
For example I want to click the background and a message will pop up " Background was tapped!" 例如,我想单击背景,然后会弹出一条消息“ 背景被轻按!”。 .
。
Do i need IBAction for that? 我需要IBAction吗?
Need help. 需要帮忙。
What you could do is use an IBAction, which would be a lot simpler code. 您可以做的是使用IBAction,这将是简单得多的代码。
In your header file of your view controller, do this: 在视图控制器的头文件中,执行以下操作:
- (IBAction)backgroundTap:(id)sender;
In your implementation file of your view controller, do this: 在视图控制器的实现文件中,执行以下操作:
- (IBAction)backgroundTap:(id)sender
{
}
In the storyboard, assuming that you have linked up view controller GUI with your view controller class, click on the background of the view controller GUI and then show the Identity Inspector in the Utilities view, which should show up on the right. 在情节提要中,假设您已将视图控制器GUI与视图控制器类链接起来,请单击视图控制器GUI的背景,然后在“实用程序”视图中显示Identity Inspector,该视图应显示在右侧。 Then, under custom class, which should currently be blank, type in UIControl.
然后,在当前应为空的自定义类下,键入UIControl。 Now go to the connections inspector and link at Touch Down to the background, selecting backgroundTap.
现在转到连接检查器,并在Touch Down处链接到背景,选择backgroundTap。 Now, anything inside the backgroundTap method will be what will happen when you select the background.
现在,backgroundTap方法中的所有内容都将是您选择背景时发生的情况。
UIButton
is the simplest way. UIButton
是最简单的方法。
- (void)backgroundButtonClicked:(id)sender
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Background was tapped!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
/*
* Your other code here
*/
UIButton *backgroundButton = [UIButton buttonWithType:UIButtonTypeCustom];
backgroundButton.backgroundColor = [UIColor clearColor];
backgroundButton.frame = self.view.bounds;
[backgroundButton addTarget:self action:@selector(backgroundButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backgroundButton];
[self.view sendSubviewToBack:backgroundButton];
}
BTW, there is no need to draw a background image because [UIImage imageNamed:@"imagename"]
returns an image. 顺便说一句,因为
[UIImage imageNamed:@"imagename"]
返回图像,所以不需要绘制背景图像。 If you want to present it, try putting the code in your -viewDidLoad
: 如果要显示它,请尝试将代码放入
-viewDidLoad
:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"backgroundimage.jpeg"]];
imageView.frame = self.view.bounds;
[self.view insertSubview:imageView belowSubview:backgroundButton];
[imageView release];
Thanks to @AlexMDC for reminding me of UITapGestureRecognizer
. 感谢@AlexMDC使我想起
UITapGestureRecognizer
。 Here is the UITapGestureRecognizer
version: 这是
UITapGestureRecognizer
版本:
- (void)tapped:(UITapGestureRecognizer *)g
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Background was tapped!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
/*
* Your other code here
*/
UITapGestureRecognizer*tap = [[UITapGestureRecognizer alloc] init];
[tap addTarget:self action:@selector(tapped:)];
[self.view addGestureRecognizer:tap];
[tap release];
}
Both versions meet the requirements. 两种版本均符合要求。 Admittedly,
UITapGestureRecognizer
is more powerful and more flexible. 诚然,
UITapGestureRecognizer
更强大,更灵活。 However, I'd prefer UIButton
to do the trick this time. 但是,这次我希望
UIButton
能够完成任务。 It's more lightweight than gesture recognizer. 它比手势识别器轻巧。 I needn't care about the state of the gesture recognizer, whether touch events have been blocked by it or how to implement the
UIGestureRecognizerDelegate
. 我不需要关心手势识别器的状态,无论触摸事件是否已被它阻止或如何实现
UIGestureRecognizerDelegate
。
It's more likely the case that we want to add some other UIView
or subclasses of UIView
on the controller's view. 它更可能是我们要添加一些其他的情况下
UIView
的类或子类UIView
在控制器的看法。 At this point, the UITapGestureRecognizer
version need to exclude all the non-background areas in – gestureRecognizerShouldBegin:
delegate method. 此时,
UITapGestureRecognizer
版本需要排除– gestureRecognizerShouldBegin:
委托方法中的所有非背景区域。
If detecting double tap is the new requirement, it is still not too late to refactor the UIButton
to the UITapGestureRecognizer
. 如果检测到双击是新的要求,那么将
UIButton
重构为UITapGestureRecognizer
仍为时不晚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.