Ios 如何平移UIView的内部图
实际上,我在UIView的子类MyView中绘制了三角形、矩形、五角大楼等图。当我触摸MyView上的任何点(无论是否在图中),MyView都会移动。我想触摸一下图的内部点,然后它必须被移动。我正在MyView上使用平移手势识别器。请向我建议 我的代码是: 在Ios 如何平移UIView的内部图,ios,uiview,touch,uipangesturerecognizer,Ios,Uiview,Touch,Uipangesturerecognizer,实际上,我在UIView的子类MyView中绘制了三角形、矩形、五角大楼等图。当我触摸MyView上的任何点(无论是否在图中),MyView都会移动。我想触摸一下图的内部点,然后它必须被移动。我正在MyView上使用平移手势识别器。请向我建议 我的代码是: 在ViewController.m中 - (void)viewDidLoad { MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(0, 100, 200, 100)
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];
}
}
- (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);
}
在MyView.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];
}
}
- (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);
}
所以,这里有一个做捏手势的代码,你想另外做一个平移手势吗?如果是这样,我建议您在viewDidLoad中创建平移手势:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(movePiece:)];
[myView addGestureRecognizer:panGesture];
[panGesture release];
然后将ivar添加到MyView类:
CGPoint _originalCenter;
最后,使用移动视图的方法:
- (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);
}
}
顺便提一下,关于您的代码有几点意见:
initWithTarget
方法实现的drawRect
方法中,我认为您需要在调用CGContextDrawPath
之前调用CGContextClosePath
无论如何,我希望通过向您展示如何使用平移手势移动子视图的示例来回答这个问题。(你说“我正在使用平移手势…”,但我想你的意思是“我想使用平移手势…”)。如果我误解了你的问题,请澄清并重新措辞,我们可以再次尝试回答它。你能澄清你的问题吗?我不明白你的应用程序目前在做什么,你希望它如何改变。您是说您的平移手势识别器正在移动整个MyView,而您更愿意只移动单个“图表”(即各种类型的多边形)?这些多边形是什么类型的对象?你是如何创造它们的?如果您澄清您的问题并发布您当前的代码,也许我们可以提供帮助。事实上,我在MyView上只绘制了一个图表,并且在MyView上应用了平移手势识别器。那么,您的平移手势识别器工作了吗?如果它正在工作,但不是你想要的方式,请描述你希望它如何改变。如果您的手势识别器不工作,请告诉我们您迄今为止尝试了什么。我真的鼓励您对(a)您所做的尝试更加明确(描述它并向我们展示代码);(b) 什么不起作用。我不知道你的问题是什么。你真的必须澄清,更详细地解释你的问题。请相应地编辑并展开您的原始问题。谢谢回复。但我想触及图表区域中的一点,然后我的视图必须移动。我有另一种方法。谢谢你为我花了宝贵的时间。