我一直在尋找幾個小時......不用說,我在編程方面頗爲新穎。 我的目標是以編程方式顯示子視圖,並通過點擊它將其刪除。添加一個UITapGestureRecognizer到子視圖
已開始工作:顯示子視圖並通過點擊視圖(代碼如下)將其刪除。
不工作 - 無論我嘗試:通過專門在子視圖上單擊刪除子視圖。
感謝您的幫助!
我的.h文件:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
UIImageView *canvas;
UIImageView *square;
}
@property (nonatomic, strong) UIImageView *canvas;
@property (nonatomic, strong) UIImageView *square;
@property (nonatomic) NSUInteger numberOfTouchesRequired;
- (void)handleSingleTap: (UITapGestureRecognizer *)singleTapGestureRecognizer;
@end
我的.m文件:
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
@synthesize canvas;
@synthesize square;
- (void)handleSingleTap: (UITapGestureRecognizer *)singleTapGestureRecognizer {
NSLog(@"tapGesture");
[square removeFromSuperview];
}
- (void)loadView {
canvas = [[UIImageView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
[canvas setImage:[UIImage imageNamed:@"canvas.png"]];
self.view = canvas;
square = [[UIImageView alloc] initWithFrame:CGRectMake(170, 320, 40, 40)];
[square setImage:[UIImage imageNamed:@"square.png"]];
[self.view setUserInteractionEnabled:YES];
[canvas addSubview:square];
NSLog(@"square");
}
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
NSLog(@"initialize");
singleTapGestureRecognizer.numberOfTapsRequired = 1;
NSLog(@"number of taps");
[self.view addGestureRecognizer:singleTapGestureRecognizer];
NSLog(@"square gestureRecognizer");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
好..對我來說最簡單的就是在這個子視圖放的UIButton(與相同尺寸的子視圖大小),並調用這個按鈕的IBAction爲刪除動作......我恨雙擊手勢... – 2014-12-07 14:28:35