2016-08-22 3 views
0

私はUIView(InsideView)でUIbezierpathを描画するための描画アプリケーションを開発しています。これは、superView(self.view)によってサブビューされています。私はtouchesBegan:, touchesMoved:, touchesEnded:のようなInsideViewの従来の描画コードを使用していますが、viewControllerクラス(InsideViewのスーパービュー)内にピンチズームコードhandlePinch:(UIPinchGestureRecognizer *)recognizer:を処理しています。なぜtouchesbegan:UIPinchGestureRecognizerを使用した後にUIViewで呼び出されることはありませんか?

最初に描画するとき、ピンチズームを行うことはできますが、ピンチズームを先に行うと、描画コード(touchesBeganなど)はUIPinchGestureRecognizerが発生した後に呼び出されず、InsideViewに何も描画されません。私は間違って何をしていますか?前もって感謝します。 (UIViewのサブクラスであるとself.viewためのサブビューです)私は、私は何の問題もなく、それらを一緒に使用するのUIViewControllerを開発しました

- (id)initWithFrame:(CGRect)frame{    
       self = [super initWithFrame:frame]; 
       if (self) { 
          path = [UIBezierPath bezierPath]; 
          [path setLineWidth:7.0]; 
          pointsArray = [NSMutableArray array]; 
          finish = NO; 
          } 
        return self; 
} 
-(void)drawRect:(CGRect)rect{ 
         [[UIColor redColor] setStroke]; 
         [path stroke]; 
} 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

        UITouch *touch = [touches anyObject]; 
        CGPoint p = [touch locationInView:self]; 
        [path moveToPoint:p]; 
        [pointsArray addObject:[NSValue valueWithCGPoint:p]]; 
} 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

        UITouch *touch = [touches anyObject]; 
        CGPoint p = [touch locationInView:self]; 
        [path addLineToPoint:p]; 
        [self setNeedsDisplay]; 
        [pointsArray addObject:[NSValue valueWithCGPoint:p]]; 
} 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 

        [path closePath]; 

        //Smoothing the path. 
        path.miterLimit=-10; 
        [self setNeedsDisplay]; 
        finish = YES; 
} 

//Code in the viewController (InsideView's superView) 
- (void)viewDidLoad { 
     [super viewDidLoad]; 
     InsideView* sV = [InsideView new]; 
     [sV setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
     [self.view addSubview:sV]; 

     //Pinch 
     UIPinchGestureRecognizer*pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)]; 
     pinch.delegate =self; 
     [sV addGestureRecognizer:pinch]; 
} 
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer{ 

     if ([recognizer numberOfTouches] < 2) 
      return; 
     if (recognizer.state == UIGestureRecognizerStateBegan) { 
      lastScale = 1.0; 
      lastPoint = [recognizer locationInView:self.view]; 
     } 
     // Scale 
     CGFloat scale = 1.0 - (lastScale - recognizer.scale); 
     [sV.layer setAffineTransform: 
     CGAffineTransformScale([sV.layer affineTransform], 
           scale, 
           scale)]; 
     lastScale = recognizer.scale; 
     // Translate 
     CGPoint point = [recognizer locationInView:self.view]; 

     [sV.layer setAffineTransform: 
     CGAffineTransformTranslate([sV.layer affineTransform], 
            point.x - lastPoint.x, 
            point.y - lastPoint.y)]; 
     lastPoint = [recognizer locationInView:self.view]; 
} 
-(void)handlePinchWithGestureRecognizer:(UIPinchGestureRecognizer *)pinchGestureRecognizer{ 
     sV.transform = CGAffineTransformScale(sV.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale); 
     pinchGestureRecognizer.scale = 1.0; 
} 
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ 
     return YES; 
} 
+2

あなたのタッチでスーパーを呼び出すようにしてください。何か方法。あなたのジェスチャ認識機能が起動したときにfinishをtrueに設定してください(ちょっと推測すれば...) – deadbeef

+1

このコード行を削除することをお勧めします。これは必要ではないので、これはあなたの問題を解決するかもしれません – ddb

+2

drawRectからピンチジェスチャーの設定を削除し、それをinitWithFrameに移動して、一度しか起こらないようにします。 drawRectで毎回ピンチャーを設定するのもばかげている。 – satheeshwaran

答えて

2

InsideViewで

//コードは、多分ねソリューションは、このロジックをUIViewからUIViewControllerに移動することができます。だから、viewDidLoadで、あなたがこれらに変更すべきか、方法handlePinch:が一緒にtouchesBegan:withEvent:touchesMoved:withEvent:で、UIViewControllerに移動する必要がありますもちろん、この

UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)]; 
[self.view addGestureRecognizer:pinch]; 

ようtouchesEnded:withEvent:方法

をピンチ認識を定義することができます方法は、あなたが

self 

を変更するべきであるということです

self.yourSpecificCurrentUIView 
+0

提案していただきありがとうございます。私は、私の図面ビューのスーパービューであるViewControllerの中にピンチ処理コードを提供し、図面ビュー(これはuiviewサブクラスです)に図面コードを提供しました。それは問題を解決しました。 –

関連する問題