簡體   English   中英

UIImageView子類無法同時工作

[英]UIImageView subclass doesn't work simultaneously gesture

我正在為iPad開發iOs 6應用程序。 我已經開發了UIImageView子類,它添加了手勢識別功能(縮放,捏和平移),但是我有一個問題:它不能同時接受手勢。 我首先將其編碼為普通的UIImageView,並且效果很好,但是現在使用子類則無法。 我的代碼:

image.m(UIImageView子類)

- (id)initWithImage:(UIImage *)image {
self = [super initWithImage:image];

if (self) {

    [self setMultipleTouchEnabled:YES];
    [self setUserInteractionEnabled:YES];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
    [super addGestureRecognizer:panRecognizer];

    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];
    [super addGestureRecognizer:pinchRecognizer];

    UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationDetected:)];
    [super addGestureRecognizer:rotationRecognizer];

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
    tapRecognizer.numberOfTapsRequired = 2;
    [super addGestureRecognizer:tapRecognizer];

    NSLog(@"lkjlkj");
}
return self;}

- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer {
CGPoint translation = [panRecognizer translationInView:self.superview];
CGPoint imageViewPosition = self.center;
imageViewPosition.x += translation.x;
imageViewPosition.y += translation.y;

self.center = imageViewPosition;
[panRecognizer setTranslation:CGPointZero inView: self.superview];}

  - (void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer{    

CGFloat scale = pinchRecognizer.scale;
self.transform = CGAffineTransformScale(self.transform, scale, scale);
pinchRecognizer.scale = 1.0;}

- (void)rotationDetected:(UIRotationGestureRecognizer *)rotationRecognizer{
CGFloat angle = rotationRecognizer.rotation;
self.transform = CGAffineTransformRotate(self.transform, angle);
rotationRecognizer.rotation = 0.0;}

- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer{
[UIView animateWithDuration:0.25 animations:^{
    self.center = CGPointMake(CGRectGetMidX(self.superview.bounds), CGRectGetMidY(self.superview.bounds));
    self.transform = CGAffineTransformIdentity;
}];}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;}

viewcontroller.m(我從其中無法同時響應多個手勢的圖像創建一個imageview)

image *img = [[image alloc] initWithImage:imatgetemporal];    
img.center = CGPointMake(525/2, 651/2);


[[self.view viewWithTag:2] addSubview:img];

謝謝大家!

我想您可以通過在聲明它們之后將相同的委托添加到UIPanGestureRecognizerUIPinchGestureRecognizerUIRotationGestureRecognizerUITapGestureRecognizer來解決。 代碼如下:

tapRecognizer.delegate = self;
pinchRecognizer.delegate = self;
rotationRecognizer.delegate = self;
tapRecognizer.delegate = self;

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM