2016-08-22 40 views
0

我正在開發一個繪圖應用程序,讓用戶在它的superView(self.view)所查看的UIView(InsideView)上繪製UIbezierpath。我在0123View中使用傳統的繪圖代碼,例如touchesBegan:, touchesMoved:, touchesEnded:,我正在處理viewController類(這是InsideView的superView)內的縮放縮放代碼handlePinch:(UIPinchGestureRecognizer *)recognizer:爲什麼touchesbegan:在使用UIPinchGestureRecognizer之後永遠不會調用UIView?

當我第一次繪製時,我可以在它後面進行縮放,但是當我首先進行縮放時,繪圖代碼(touchesBegan等)在UIPinchGestureRecognizer被觸發後不會被調用,並且它不會在InsideView上繪製任何東西。我究竟做錯了什麼?提前致謝。

//代碼在InsideView(這是一個UIView子類,是self.view子視圖)

- (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

嘗試在touches中調用超級方法。當你的手勢識別器觸發時(也只是一個猜測),你也可以設置完成爲true – deadbeef

+1

我建議你刪除這條代碼行'pinch.delegate = self;',因爲它不是必要的,也許這就解決了你的問題 – ddb

+2

你可以試試從drawRect刪除捏手勢設置,並將其移動到initWithFrame,使它只發生一次?每次在drawRect上設置pinch也是很荒謬的。 – satheeshwaran

回答

2

我已經開發了一個UIViewController,我同時使用他們沒有任何問題,S也許解決方案可能是將此邏輯從UIView移動到UIViewController。因此,在viewDidLoad你可以touchesBegan:withEvent:定義捏識別這樣

UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)]; 
[self.view addGestureRecognizer:pinch]; 
當然也是方法 handlePinch:應該被移到 UIViewController

,在一起, touchesMoved:withEvent:touchesEnded:withEvent:方法

你應該在這些改變什麼方法是,你應該改變

self 

self.yourSpecificCurrentUIView 
+0

感謝您的建議,我在ViewController裏面提供了捏合處理代碼,這是我的圖紙視圖的超級視圖,並且我在圖紙視圖(這是一個uiview子類)中提供了圖形代碼。它確實解決了這個問題。 –

相關問題