我想弄清楚如何首先选择一个对象然后我能够移动它。例如,我想首先通过触摸选择牛,然后我可以移动它。原因是当我触摸屏幕时,它正在移动牛和牛1。当我只想一次移动一头牛时。任何帮助将不胜感激。
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
cow.center = CGPointMake(touchLocation.x, touchLocation.y);
cow1.center = CGPointMake(touchLocation.x, touchLocation.y);
}
答案 0 :(得分:0)
有很多方法可以实现这类功能。这取决于您的图形的实现方式,除其他外,请查看此Apple示例代码:
http://developer.apple.com/library/ios/#samplecode/Touches/Introduction/Intro.html
答案 1 :(得分:0)
标准解决方案是为您的两个牛对象添加单独的手势识别器,例如viewDidLoad
:
UIPanGestureRecognizer *panCow1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveCow:)];
[cow1 addGestureRecognizer:panCow1];
// if non ARC, make sure to add a line that says
// [panCow1 release];
UIPanGestureRecognizer *panCow2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveCow:)];
[cow2 addGestureRecognizer:panCow2];
// if non ARC, make sure to add a line that says
// [panCow2 release];
然后你的moveCow
方法看起来像:
- (void)moveCow:(UIPanGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateChanged)
{
CGPoint translate = [sender translationInView:self.view];
sender.view.center = CGPointMake(sender.view.center.x + translate.x, sender.view.center.y + translate.y);
}
}
我没有测试过这段代码,但是你明白了。这就是我通常在...周围移动单独的子视图的方式。
答案 2 :(得分:0)
尝试使用UIPanGestureRecognizer,如下所示:
@synthesize myImage; //UIImageView
- (void)viewDidLoad
{
[super viewDidLoad];
[self startMoveImage];
}
-(void) startMoveImage{
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.view addGestureRecognizer:panGestureRecognizer];
}
- (void)pan:(UIPanGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
CGPoint position = [gesture locationInView:[myImage superview]];
[myImage setCenter:position];
}
}