私は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;
}
あなたのタッチでスーパーを呼び出すようにしてください。何か方法。あなたのジェスチャ認識機能が起動したときにfinishをtrueに設定してください(ちょっと推測すれば...) – deadbeef
このコード行を削除することをお勧めします。これは必要ではないので、これはあなたの問題を解決するかもしれません – ddb
drawRectからピンチジェスチャーの設定を削除し、それをinitWithFrameに移動して、一度しか起こらないようにします。 drawRectで毎回ピンチャーを設定するのもばかげている。 – satheeshwaran