我正在開發一個繪圖應用程序,讓用戶在它的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;
}
嘗試在touches中調用超級方法。當你的手勢識別器觸發時(也只是一個猜測),你也可以設置完成爲true – deadbeef
我建議你刪除這條代碼行'pinch.delegate = self;',因爲它不是必要的,也許這就解決了你的問題 – ddb
你可以試試從drawRect刪除捏手勢設置,並將其移動到initWithFrame,使它只發生一次?每次在drawRect上設置pinch也是很荒謬的。 – satheeshwaran