Ios 使用CGAffineTransformTranslate通过触摸在特定区域移动图像
我想在屏幕的特定区域移动图像。我不想弄乱框架,因为它会在以后的碰撞行为中造成麻烦。到目前为止我已经得到了这个代码。我不知道如何使用触摸。谢谢你的帮助Ios 使用CGAffineTransformTranslate通过触摸在特定区域移动图像,ios,objective-c,cgaffinetransform,Ios,Objective C,Cgaffinetransform,我想在屏幕的特定区域移动图像。我不想弄乱框架,因为它会在以后的碰撞行为中造成麻烦。到目前为止我已经得到了这个代码。我不知道如何使用触摸。谢谢你的帮助 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { //CGPoint svpt = [[touches anyObject] locationInView:self.view]; CGPoint pt = [[touches anyObject] lo
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//CGPoint svpt = [[touches anyObject] locationInView:self.view];
CGPoint pt = [[touches anyObject] locationInView:self.playerMove];
CGRect frame = [self.playerMove frame];
CGAffineTransform CGAT = CGAffineTransformIdentity;
CGAT = CGAffineTransformTranslate(CGAT, MIN(MAX(frame.origin.x, -10), 240), MIN(MAX(frame.origin.y, 430), 430));
self.playerMove.transform = CGAT;
}
您可以使用平移手势识别器
UIPanGestureRecognizer *gr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.playerMove addGestureRecognizer:gr];
self.playerMove.userInteractionEnabled = YES;
后来
- (void)handlePan:(UIPanGestureRecognizer *)gr {
if (gr.state == UIGestureRecognizerStateBegan || gr.state == UIGestureRecognizerStateChanged) {
UIView *view = gr.view;
CGPoint translation = [gr translationInView:view];
view.transform = CGAffineTransformTranslate(view.transform, translation.x, translation.y);
[gr setTranslation:CGPointZero inView:view];
}
}
图像没有移动…我调用了viewDidLoad中的前两行代码,并使用handlePan方法,以及如何为我要移动图像的区域设置边界??嗯。我刚刚测试过,虽然不完善。这对我有用。请确保playerMove视图的userInteractionEnabled=YES。编辑答案以添加有关userInteractionEnabled的提醒,还改进了pan代码以重置识别之间的转换状态。你能给我写下你的邮件地址吗,我会给你发一个项目,只需几行代码,如果你能看一下,谢谢你的回复,然后把链接贴在这里。我应该能在今天结束前赶到。