2

アラート メニューの条件付きボタンを使用して、シングル タップ ジェスチャで追加されるサブビューを追加または削除したいと考えています。また、条件を追加したばかりのサブビュー (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
4

1 に答える 1

0

ヘッダー ファイルで imgView を宣言します。

 @property(nonatomic,strong)UIImageView *imgView;

クラスの他のメソッドからアクセスできるようにします。その後、必要なことは何でもできます(addSubview、removeFromSuperView、imgView.hidden = YESなど...)

于 2013-03-19T14:32:21.450 に答える