我想使用警报菜单条件按钮来添加或删除通过单击手势添加的子视图。另外,我希望条件仅适用于刚刚添加的子视图(imgView),而不适用于之前添加的任何其他子视图。我是开发新手,可以使用一些帮助。提前致谢。以下代码是我的 ViewController.m 文件。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(IBAction)controlPan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x +translation.x,recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0,0) inView:self.view];
}
# pragma mark - View Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleDoubleTap:)];
[doubleTapGestureRecognizer setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:doubleTapGestureRecognizer];
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleSingleTap:)];
[singleTapGestureRecognizer setNumberOfTapsRequired:1];
// Wait for failed doubleTapGestureRecognizer
[singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGestureRecognizer];
[self.view addGestureRecognizer:singleTapGestureRecognizer];
}
# pragma mark - Tap Gestures
- (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer {
// Inserts picture xx.png at double-tapped location
CGPoint location = [recognizer locationInView:[self view]];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(location.x,location.y, 20, 20)];
UIImage *image = [UIImage imageNamed:@"xx.png"];
[imgView setImage:image];
[self.view addSubview:imgView];
}
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
// Inserts picture x.png at single tapped location if user selects "yes" on alert popup
// If user selects "cancle" from alert popup nothing happens
CGPoint location = [recognizer locationInView:[self view]];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(location.x,location.y, 20, 20)];
UIImage *img = [UIImage imageNamed:@"x.png"];
[imgView setImage:img];
[self.view addSubview:imgView];
NSString *msg = @"Add Hold Here?";
//sends alert popup to ask if hold location is correct
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:@"" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes",nil];
[alert show];
}
#pragma mark - Alerts
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex:(UIImageView *)imgView
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Cancel"])
{
//Will remove the imgView subview that was just created location
NSLog(@"Button 1 was selected.");
}
else if([title isEqualToString:@"Yes"])
{
//Will keep the imgView subview that was just created at location
NSLog(@"Button 2 was selected.");
}
}
@end