[英]How to pan inside diagram of UIView
Actually, i draw a diagrams like triangle, rectangle, pentagon in MyView which subclass of UIView. 实际上,我在MyView的UIView的子类中绘制了三角形,矩形,五边形等图。 When i touch any point on MyView (ether that point inside diagram or not), MyView is moved.
当我触摸MyView上的任何点时(无论该点是否在图表中),MyView都会移动。 I would like to touch inside point of diagram then it has to be moved.
我想触摸图表的内部,然后将其移动。 I am using pan gesture recognizer on MyView.Please suggest to me.
我在MyView上使用平移手势识别器。请向我建议。
My Code is: 我的代码是:
In ViewController.m
, 在
ViewController.m
,
- (void)viewDidLoad
{
MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(0, 100, 200, 100)];
[self.view addSubview:myView];
[super viewDidLoad];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePiece:)];
[pinchGesture setDelegate:self];
[myView addGestureRecognizer:pinchGesture];
[pinchGesture release];
}
- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
[gestureRecognizer setScale:1];
}
}
In MyView.m
, 在
MyView.m
,
- (void)drawRect:(CGRect)rect
{
// Drawing code
context =UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
// And draw with a blue fill color
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, 50.0, 10.0);
CGContextAddLineToPoint(context, 5.0, 70.0);
CGContextAddLineToPoint(context, 150.0, 55.0);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextClosePath(context);
CGContextStrokePath(context);
}
So, you have code here that does a pinch gesture and you want to additionally do a pan gesture? 因此,您这里有执行捏手势的代码,并且还想做平移手势吗? If so, I would suggest you create the pan gesture in viewDidLoad:
如果是这样,我建议您在viewDidLoad中创建平移手势:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(movePiece:)];
[myView addGestureRecognizer:panGesture];
[panGesture release];
Then add an ivar to your MyView class: 然后将一个ivar添加到MyView类中:
CGPoint _originalCenter;
And, finally, have the method to move your view: 最后,拥有移动视图的方法:
- (void)movePiece:(UIPanGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan)
{
_originalCenter = [gestureRecognizer view].center;
}
else if ([gestureRecognizer state] == UIGestureRecognizerStateChanged)
{
CGPoint translation = [gestureRecognizer translationInView:self.view];
[gestureRecognizer view].center = CGPointMake(_originalCenter.x + translation.x, _originalCenter.y + translation.y);
}
}
As an aside, a few observations regarding your code: 顺便说一句,关于您的代码的一些观察:
You're setting your pinch gesture's delegate, but that's not necessary. 您正在设置捏手势的代表,但这不是必需的。 That's accomplished by the
initWithTarget
method. 这是通过
initWithTarget
方法完成的。
In your drawRect
method, I think you want to call CGContextClosePath
before you call CGContextDrawPath
. 在您的
drawRect
方法中,我认为您想在调用CGContextClosePath
之前先调用CGContextDrawPath
。
Regardless, I hope I answered the question by showing you an example of how you could use a pan gesture to move your subview. 无论如何,我希望我通过向您展示一个如何使用平移手势移动子视图的示例来回答这个问题。 (You said "I am using pan gesture..." but I assumed you meant "I would like to use pan gesture...".) If I misunderstood your question, then please clarify and rephrase the question and we can take another crack at answering it.
(您说“我正在使用摇动手势...”,但我假设您的意思是“我想使用摇动手势...”。)如果我误解了您的问题,请澄清并重新表述该问题,我们可以再提出一个问题破解它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.