为什么我的视图无法识别 UIPanGestureRecognizer?
Why my view can not recognize the UIPanGestureRecognizer?
我给图片打标签的时候,没有东西可以显示?为什么?
userInteractionEnabled
属性的默认值在UIImageView
class中设置为NO
。也许这是你的问题。
This property is inherited from the UIView parent class. This class changes the default value of this property to NO.
检查了这个片段代码:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:imageView];
imageView.userInteractionEnabled = YES;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)];
[imageView addGestureRecognizer:panGesture];
}
- (void)panGestureRecognized:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"It Works!");
}
应用此代码运行良好:
-(void)initialization
UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)] autorelease];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[imageview addGestureRecognizer:panRecognizer];
imageview.userInteractionEnabled = YES;
}
- (void)move:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = imageview;
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
CGPoint translatedCenter = CGPointMake([piece center].x + translation.x, [piece center].y + translation.y);
CGPoint center = [self centerWithBounds:translatedCenter andViewFrame:[piece frame] andBoundingFrame:[[piece superview] frame]];
[piece setCenter:center];
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
_view2 = [[V0 alloc ]initWithFrame:self.view.bounds];
CGRect a = self.view2.frame;
a.origin.x = 100;
a.origin.y = 150;
_view2.frame = a;
_view2.userInteractionEnabled = YES;
[self.view2 addGestureRecognizer:[[UIPanGestureRecognizer alloc]initWithTarget:self.view2 action:@selector(Up:)]];
我添加了 initWithFrame ,然后我的视图可以识别我的手势
我给图片打标签的时候,没有东西可以显示?为什么?
userInteractionEnabled
属性的默认值在UIImageView
class中设置为NO
。也许这是你的问题。
This property is inherited from the UIView parent class. This class changes the default value of this property to NO.
检查了这个片段代码:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:imageView];
imageView.userInteractionEnabled = YES;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)];
[imageView addGestureRecognizer:panGesture];
}
- (void)panGestureRecognized:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"It Works!");
}
应用此代码运行良好:
-(void)initialization
UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)] autorelease];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[imageview addGestureRecognizer:panRecognizer];
imageview.userInteractionEnabled = YES;
}
- (void)move:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = imageview;
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
CGPoint translatedCenter = CGPointMake([piece center].x + translation.x, [piece center].y + translation.y);
CGPoint center = [self centerWithBounds:translatedCenter andViewFrame:[piece frame] andBoundingFrame:[[piece superview] frame]];
[piece setCenter:center];
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
_view2 = [[V0 alloc ]initWithFrame:self.view.bounds];
CGRect a = self.view2.frame;
a.origin.x = 100;
a.origin.y = 150;
_view2.frame = a;
_view2.userInteractionEnabled = YES;
[self.view2 addGestureRecognizer:[[UIPanGestureRecognizer alloc]initWithTarget:self.view2 action:@selector(Up:)]];
我添加了 initWithFrame ,然后我的视图可以识别我的手势