如何在xcode 5中添加垂直手势

时间:2014-03-16 16:20:55

标签: ios iphone objective-c

如何在图像上的UIPanGestureRecognizer中添加直线垂直幻灯片。我有一些代码:

- (IBAction)HandlePan:(UIPanGestureRecognizer *)panner
{
    CGPoint translation = [panner translationInView:panner.view];
    CGPoint newCenter = CGPointMake(self.pesawat.center.x + translation.x, self.pesawat.center.y);

    int offset = 80; // 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];
} 

如果我将代码改为这样:

CGPoint newCenter = CGPointMake(self.pesawat.center.x + translation.x, self.pesawat.center.y + translation.y);

它变得可以拖动(水平,垂直,对角线),我想要的只是使它垂直和水平直线而没有图像移动对角线

和这个viewdidload:

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

任何人都可以提示吗?

1 个答案:

答案 0 :(得分:0)

CGPoint translation = [panner translationInView:panner.view];
if (translation.x > translation.y)
    translation.y = 0.0f;
else
    translation.x = 0.0f;

CGPoint newCenter = CGPointMake(self.pesawat.center.x + translation.x, self.pesawat.center.y + translation.y);

这仅适用于translation的较大部分。