使用fix x,y iOS7在手指后移动图像

时间:2014-03-15 14:34:34

标签: ios iphone objective-c

如何在 xcode 5 中使用修复x,y坐标平滑(跟随用户手指)移动图像。 我已经制作了一些代码,但它并没有按照我的手指动作移动(图像立即移动到新位置) 我的.m

#import "ViewController.h"
#import "MyScene.h"

@interface ViewController()
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;
@end

@implementation ViewController

- (IBAction)HandlePan:(UIPanGestureRecognizer *)recognizer{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0,0) inView:self.view];
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];

self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;

[self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
[self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
}

- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
    CGPoint labelPosition = CGPointMake(self.pesawat.frame.origin.x - 30.0, self.pesawat.frame.origin.y);
    self.pesawat.frame = CGRectMake( labelPosition.x , labelPosition.y , self.pesawat.frame.size.width, self.pesawat.frame.size.height);
 }

if (sender.direction == UISwipeGestureRecognizerDirectionRight)
{
    CGPoint labelPosition = CGPointMake(self.pesawat.frame.origin.x + 30.0, self.pesawat.frame.origin.y);
 self.pesawat.frame = CGRectMake( labelPosition.x , labelPosition.y , self.pesawat.frame.size.width, self.pesawat.frame.size.height);
}
}

和我的.h

    @interface ViewController : UIViewController
    @property (nonatomic, retain) IBOutlet UIImageView *pesawat;
    @end

3 个答案:

答案 0 :(得分:1)

作为一个非常基本的例子,

您应该使用panGestureRecognizer:

UIPanGestureRecognizer * panner = [[UIPanGestureRecognizer alloc]init];
[panner addTarget:self action:@selector(handlePan:)];
[self.view addGestureRecognizer:panner];

然后:

- (void) handlePan:(UIPanGestureRecognizer *)panner {

    CGPoint translation = [panner translationInView:panner.view];
    CGPoint newCenter = CGPointMake(self.pesawat.center.x + translation.x, self.pesawat.center.y);

    int offset = 30; // pixels to offset

    if (newCenter.x < offset) newCenter = CGPointMake(offset, newCenter.y);
    else if (newCenter.x > self.view.bounds.size.width - offset) newCenter = CGPointMake(self.view.bounds.size.width - offset, newCenter.y);

    self.pesawat.center = newCenter;
    [panner setTranslation:CGPointMake(0, 0) inView:panner.view];
}

答案 1 :(得分:0)

我使用Long Press Gesture识别器进行此操作。像这样:

UILongPressGestureRecognizer *ges = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lp:)];
ges.minimumPressDuration = 0.1;
self.pesawat.userInteractionEnabled = YES;
[self.pesawat addGestureRecognizer:ges];

然后添加:

-(void)lp:(UILongPressGestureRecognizer*)ges{
    float x = ([ges locationInView:self.GraphicBox].x);
    float y = ([ges locationInView:self.GraphicBox].y);
    [imgView setCenter:CGPointMake(x,y)];
}

这应该适合你。

答案 2 :(得分:0)

我建议你保持y位置,改变x

basketView.center = CGPointMake(point.x, basketView.center.y);

或者我建议您按照这些教程进行操作:

1) http://blog.spritebandits.com/2012/04/17/iphone-drawing-on-screen-follow-a-finger/

2) http://www.raywenderlich.com/44270/sprite-kit-tutorial-how-to-drag-and-drop-sprites

希望此信息可以帮助您。