我已经构建了一个小型测试应用程序,可以在UIButton上应用平移手势。我成功应用了平移手势并成功移动了按钮。 但问题是我甚至可以在屏幕外移动按钮。 如何限制它仅在iPhone屏幕内移动? 这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[panGesture setMinimumNumberOfTouches:1];
[_shareButton addGestureRecognizer:panGesture];
}
-(IBAction)pan:(UIPanGestureRecognizer *)recognizer
{
CGPoint trans =[recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x+trans.x, recognizer.view.center.y+trans.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
如何限制按钮移出屏幕?我使用的是iOS 7,xcode 5.0。
答案 0 :(得分:4)
这对你有用..但我用 Swift :
写了func callMe(sender: UIPanGestureRecognizer)
{
let translation = sender.translationInView(self.view)
let newX = sender.view!.center.x + translation.x
let newY = sender.view!.center.y + translation.y
let senderWidth = sender.view!.bounds.width / 2
let senderHight = sender.view!.bounds.height / 2
if newX <= senderWidth
{
sender.view!.center = CGPoint(x: senderWidth, y: sender.view!.center.y + translation.y)
}
else if newX >= self.view.bounds.maxX - senderWidth
{
sender.view!.center = CGPoint(x: self.view.bounds.maxX - senderWidth, y: sender.view!.center.y + translation.y)
}
if newY <= senderHight
{
sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: senderHight)
}
else if newY >= self.view.bounds.maxY - senderHight
{
sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: self.view.bounds.maxY - senderHight)
}
else
{
sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
}
sender.setTranslation(CGPointZero, inView: self.view)
}
我希望它可以帮助那些寻找最简单方法的人。
答案 1 :(得分:0)
像这样编辑你的代码:
-(IBAction)pan:(UIPanGestureRecognizer *)recognizer
{
CGPoint trans =[recognizer translationInView:self.view];
if (CGRectContainsPoint([self.view frame], trans))
{
recognizer.view.center = trans;
}
}
这里你要做的是,你是否是在视图的框架中。如果是。你正在移动拖动按钮的中心..如果这不能解决你的问题,请告诉我。
更新2:
您也可以使用 onDrag事件,而不是实现PanGesture:
- (IBAction)onButtonDrag:(id)sender forEvent:(UIEvent *)event {
CGPoint point = [[[event allTouches] anyObject] locationInView:sender.view];
UIControl *control = sender;
if (CGRectContainsPoint([self.view frame], point)) {
control.center = point;
[self.view touchesEnded:[event allTouches] withEvent:event];
}
}
答案 2 :(得分:0)
试试这个:
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[pan setDelegate:self];
[shareButton addGestureRecognizer:pan];
-(void)handlePan:(UIGestureRecognizer*)panGes{
CGPoint point = [panGes locationInView:self.view];
if (point.x >20 && point.x <280 && point.y<400 && point.y>30) {
CGRect newframe = CGRectMake(point.x, point.y, shareButton.frame.size.width, shareButton.frame.size.height);
shareButton.frame = newframe;
}
}
并替换20,280&amp; 400,30与您各自的限制。
答案 3 :(得分:0)
你的手势框架的绑定限制。它的大小只是window.width和height。
下面是手势教程。我知道它是用于游戏但是在这个教程中找到你的逻辑。
http://www.raywenderlich.com/2343/cocos2d-tutorial-for-ios-how-to-drag-and-drop-sprites
<强> Disabling Pan Gesture if out of bounds detected 强>
- (CGPoint)boundLayerPos:(CGPoint)newPos {
CGSize winSize = [CCDirector sharedDirector].winSize;
CGPoint retval = newPos;
retval.x = MIN(retval.x, 0);
retval.x = MAX(self.position.x);
retval.y = self.position.y;
return retval;
}
答案 4 :(得分:0)
知道了。编码如下:
-(IBAction)pan:(UIPanGestureRecognizer *)recognizer
{
CGPoint trans =[recognizer translationInView:self.view];
CGFloat sumx = _shareButton.frame.origin.x+_shareButton.frame.size.width+5;
CGFloat sumy = _shareButton.frame.origin.y+_shareButton.frame.size.height+5;
CGFloat sumxm = _shareButton.frame.origin.x;
CGFloat sumym = _shareButton.frame.origin.y;
NSLog(@"position : %f, %f ", _shareButton.frame.origin.x, _shareButton.frame.origin.y);
NSLog(@"screen : %f, %f ", self.view.frame.size.width ,self.view.frame.size.height);
if (sumx > self.view.frame.size.width || sumy > self.view.frame.size.height )
{
CGRect shareButton = _shareButton.frame;
shareButton.origin.y -= 1;
shareButton.origin.x -= 1;
_shareButton.frame = shareButton;
return;
}
else if(sumxm < 0 || sumym < 0) {
CGRect shareButton = _shareButton.frame;
if(sumxm < 0)
{
shareButton.origin.x =0;
}
if(sumym < 0){
shareButton.origin.y =0;
}
_shareButton.frame = shareButton;
NSLog(@"share button : %f, %f", _shareButton.frame.origin.x, _shareButton.frame.origin.y);
return;
}
recognizer.view.center = CGPointMake(recognizer.view.center.x+trans.x, recognizer.view.center.y+trans.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
答案 5 :(得分:0)
-(void)dragAction:(UIPanGestureRecognizer *)gesture{
UIView *vw = (UIView *)gesture.view;
CGPoint translation = [gesture translationInView:vw];
if (CGRectContainsPoint(vw.frame, [gesture locationInView:vw] )) {
vw.center = CGPointMake(vw.center.x,
vw.center.y);
[gesture setTranslation:CGPointZero inView:vw];
}
else{
vw.center = CGPointMake(vw.center.x,
vw.center.y + translation.y);
[gesture setTranslation:CGPointZero inView:vw];
}