0

I have a button that can be dragged around on the screen. I was wondering if there is a way to keep the button inside the frame of the view.

I have used this code to make the button drag able:

UIPanGestureRecognizer *buttonPanRecognizer;
buttonPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(setObjectLocation:)];
[NewButton addGestureRecognizer:buttonPanRecognizer];

- (void)setObjectLocation:(UIPanGestureRecognizer *)recognizer {

CGPoint location = [recognizer locationInView:self.view];

if (CGRectContainsPoint([NewButton frame], location)) {              // NewButton
    NewButton.center = location;
} 
else if (CGRectContainsPoint([NewLabel frame], location)) {          // NewLabel
    NewLabel.center = location;
} }

I also want to be able to keep other kinds of objects inside.

Thansk in advance :)

enter image description here

The problem is that it is possible to drag parts of the UIButton outside the screen.

5
  • I suspect that this question isn't really related to Xcode, isn't it? You want to accomplish what you are doing inside your app and not inside Xcode (like in Interface Builder), right? Oct 9, 2011 at 21:00
  • ehhh a little confused over you question. But what I need is the code to write in Xcode so that when my app is running on a device and someone is dragging a button around it can't be moved outside the frame of the view.... of course it can't be done to drag the entire object out of the frame but parts of it can't Oct 9, 2011 at 21:08
  • as it is now... and I wishes that the none of the parts of the object can go outside the frame of the view Oct 9, 2011 at 21:10
  • I know what you want to do, I am just telling you that this is not an Xcode related question (other than the fact that you are writing your code in Xcode, but you could use any text editor to do that). It's like saying: 'Microsoft Word: How do I spell Blabla'. But a friendly moderator already removed the Xcode tag, you should consider also removing it from your headline. Oct 9, 2011 at 21:20
  • Okay thanks... but that still doesn't quite solves my problem.... do you know how I could do it inside Interface Builder? :) Oct 9, 2011 at 22:11

2 Answers 2

1

I have to agree that your problem is not quite clear. What is all the stuff with the label supposed to to. Generally, to keep a button inside a frame, you have everything you need already there:

UIPanGestureRecognizer* buttonPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(setObjectLocation:)];
[NewButton addGestureRecognizer:buttonPanRecognizer];

- (void)setObjectLocation:(UIPanGestureRecognizer *)recognizer {

    CGPoint location = [recognizer translationInView:self.view];

    if (CGRectContainsPoint(self.view.frame, location)) {
       NewButton.center = location;
    } 
}
5
  • I have put in a photo so that you can see the problem. Oct 12, 2011 at 21:56
  • That's not really helpful. What does my solution do? You know, you can't expect us to give you code that you just need to copy and paste. You will need to test and maybe modify it to fit your code, if it doesn't work. Oct 12, 2011 at 22:01
  • I know... I'll try to play around with it a little :) thanks :) Oct 12, 2011 at 22:13
  • No problem. Also, and I really don't mean this as an offense, you need to get you basics straight. Judging by this and your other question, you have some gaps in you knowledge about basic obj-c. There are many 'Just type in what I say' tutorials and following them is a great way dive into the matter :) Oct 12, 2011 at 22:18
  • okay thanks :) i'm updating my other question in a few minutes, about how I have done some things... I would appreciate it if you would look at it :) Oct 12, 2011 at 22:30
0

What's the problem? Just keep the button's origin within a rect (frame.x,frame.y,frame.width - button.width,frame.height - button.height).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.