Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 01-07-2012, 11:32 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Broome, Western Australia
Posts: 21
Scruffy is on a distinguished road
Cool Reset Button for draggable images

I am creating a fairly simple app with several draggable images in a view, and I want a reset button which will reset all the images to their original positions. Can anyone help with the code for this, or point me to some useful resources?

Thanks in advance.

ps my code in the .m file looks like this:


- (void)touchesMovedNSSet *)touches withEventUIEvent *)event {

UITouch *touch = [[event allTouches] anyObject];

if ([touch view] == img13) {
CGPoint location = [touch locationInView:self.view];
img13.center = location;
}

if ([touch view] == img14) {
CGPoint location = [touch locationInView:self.view];
img14.center = location;
}

if ([touch view] == img15) {
CGPoint location = [touch locationInView:self.view];
img15.center = location;
}
Scruffy is offline   Reply With Quote
Old 01-07-2012, 11:40 PM   #2 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 154
Bobarino is on a distinguished road
Default

Code:
- (IBAction)buttonPressed {
image.center = CGPointMake (x,y);
}
__________________


Island Hop

Website- KewlPro
Bobarino is offline   Reply With Quote
Old 01-08-2012, 06:40 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Broome, Western Australia
Posts: 21
Scruffy is on a distinguished road
Default Thankyou!

Hi there,
Excellent, i got it working now. I have just one other issue i am unable to solve so far; when i grab an image to drag it, as soon as i start moving it the image jumps so that it's centre moves to the cursor [on my IOS Simulator]. The drag action still works, it just looks a bit sloppy, or jumpy, instead of smooth. Any ideas?

(Thanks Bobarino


Quote:
Originally Posted by Bobarino View Post
Code:
- (IBAction)buttonPressed {
image.center = CGPointMake (x,y);
}
Scruffy is offline   Reply With Quote
Old 01-08-2012, 07:40 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Default

Instead of
Code:
CGPoint location = [touch locationInView:self.view];
img.center = location;
use
Code:
CGPoint location = [touch locationInView:self.view];
[UIView animateWithDuration:0.3 animations:^{
	img.center = location;
}];
architectpianist is offline   Reply With Quote
Old 01-08-2012, 11:54 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Broome, Western Australia
Posts: 21
Scruffy is on a distinguished road
Default image still jumps at start of drag...

Thanks architectpianist, but i can't get that code to work; the image can be dragged, but it still jumps when i begin dragging it.

scruffy



Quote:
Originally Posted by architectpianist View Post
Instead of
Code:
CGPoint location = [touch locationInView:self.view];
img.center = location;
use
Code:
CGPoint location = [touch locationInView:self.view];
[UIView animateWithDuration:0.3 animations:^{
	img.center = location;
}];
Scruffy is offline   Reply With Quote
Old 01-10-2012, 01:35 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,002
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Scruffy View Post
Thanks architectpianist, but i can't get that code to work; the image can be dragged, but it still jumps when i begin dragging it.

scruffy
I would suggest not moving the image location to the point the user touches, but instead, detecting a dragging motion, and offsetting the image location by the amount the user drags.

UIGestureRecognizers make this much easier. Do a search in Xcode on "touches" and download the sample app with that name. That will get you 2 projects, one that does it the old way, with touchesBegan/touchesMoved, and the other that uses gesture recognizers. Take a look at the gesture recognizer one.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 01-15-2012, 12:35 AM   #8 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Broome, Western Australia
Posts: 21
Scruffy is on a distinguished road
Default Some progress...

Hi Guys,
Thanks for your help. I have followed all advice and tried out all the above suggestions but unfortunately i don't know enough to make any of them work. However i found an example by Navaneethan Thambu called MyDragApp which i can get to work so i will follow that trail for the moment. It uses the older method but it works and as a novice at this thats all i can go by atm.

Thanks again, and catch you all later
Scruffy is offline   Reply With Quote
Old 01-16-2012, 05:52 AM   #9 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Broome, Western Australia
Posts: 21
Scruffy is on a distinguished road
Default Touches in Landscape Mode?

Hi Duncan,
Regarding the Touches app that you recommended, i have found some of the things i need in an app, using UIGestureRecognizers, but i cannot set Touches into landscape mode. I can make it turn to the right, but the images move 90 degrees as well. I have spent some days on this, exploring lots of image movement apps trying to find one that does all the things i need it to do and i have found all the elements i need, but not all in the one app. Anyway, thanks for your help.


Quote:
Originally Posted by Duncan C View Post
I would suggest not moving the image location to the point the user touches, but instead, detecting a dragging motion, and offsetting the image location by the amount the user drags.

UIGestureRecognizers make this much easier. Do a search in Xcode on "touches" and download the sample app with that name. That will get you 2 projects, one that does it the old way, with touchesBegan/touchesMoved, and the other that uses gesture recognizers. Take a look at the gesture recognizer one.
Scruffy is offline   Reply With Quote
Old 01-16-2012, 08:46 AM   #10 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,002
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Scruffy View Post
Hi Duncan,
Regarding the Touches app that you recommended, i have found some of the things i need in an app, using UIGestureRecognizers, but i cannot set Touches into landscape mode. I can make it turn to the right, but the images move 90 degrees as well. I have spent some days on this, exploring lots of image movement apps trying to find one that does all the things i need it to do and i have found all the elements i need, but not all in the one app. Anyway, thanks for your help.

Dragging with gesture recognizers has very little to do with auto-rotation. Auto-rotation is a separate issue. If you find one example of how to do it, it's a simple matter to adapt the technique to most applications.

You're unlikely to find a single sample app that does exactly what you want. You need to be able to use samples to learn new techniques, and then adapt and synthesize those techniques into new applications.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 01-16-2012, 10:05 AM   #11 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Broome, Western Australia
Posts: 21
Scruffy is on a distinguished road
Default

I know that the following code normally allows for auto-rotation but i just wonder why it doesn't work on the Touches app.

- (BOOL)shouldAutorotateToInterfaceOrientationUIIn terfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (YES);
}




Quote:
Originally Posted by Duncan C View Post
Dragging with gesture recognizers has very little to do with auto-rotation. Auto-rotation is a separate issue. If you find one example of how to do it, it's a simple matter to adapt the technique to most applications.

You're unlikely to find a single sample app that does exactly what you want. You need to be able to use samples to learn new techniques, and then adapt and synthesize those techniques into new applications.
Scruffy is offline   Reply With Quote
Old 01-16-2012, 10:39 AM   #12 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,002
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Scruffy View Post
I know that the following code normally allows for auto-rotation but i just wonder why it doesn't work on the Touches app.

- (BOOL)shouldAutorotateToInterfaceOrientationUIIn terfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (YES);
}

Touches is a dirt-simple application. It doesn't even have a view controller. (I'm a little surprised the system doesn't complain about that. Newer OS versions expect your app to create a view controller on launch.)

The method shouldAutorotateToInterfaceOrientation is a view controller method.

If you create an app with a view controller, and add your shouldAutorotateToInterfaceOrientation method above, then it should allow the UI to rotate to all user interface orientations. You then need set up the "struts and springs" (resize controls) in your views so that they resize as the window size changes. For most apps, that's all there is to it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 01-17-2012, 05:55 PM   #13 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Broome, Western Australia
Posts: 21
Scruffy is on a distinguished road
Default Pan Gesture Recognizers

Finally got it, thanks to a lot of help and a tutorial. Added Pan Gesture Recognizers from the objects library to the image containers on the storyboard, added some code and hooked it up and it works!

The code looks like this:

In ViewController.h ---->

- (IBAction)handlePanUIPanGestureRecognizer *)recognizer;


In ViewController.m ---->


- (IBAction)handlePanUIPanGestureRecognizer *)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];

}

Then you select the UIPanGestureRecongizer in Interface Builder, bring up the Connections inspector, and drag a line from the selector to the View Controller, and choose handlePan and thats it.
Now everything works smoothly, and it rotates too

Thanks again
Scruffy




Quote:
Originally Posted by Duncan C View Post
Touches is a dirt-simple application. It doesn't even have a view controller. (I'm a little surprised the system doesn't complain about that. Newer OS versions expect your app to create a view controller on launch.)

The method shouldAutorotateToInterfaceOrientation is a view controller method.

If you create an app with a view controller, and add your shouldAutorotateToInterfaceOrientation method above, then it should allow the UI to rotate to all user interface orientations. You then need set up the "struts and springs" (resize controls) in your views so that they resize as the window size changes. For most apps, that's all there is to it.
Scruffy is offline   Reply With Quote
Old 04-02-2012, 04:14 PM   #14 (permalink)
kmt
Registered Member
 
Join Date: Oct 2011
Posts: 29
kmt is on a distinguished road
Default

Hi Scuffy/Duncan,

So, I am using this same code but, getting some very odd reactions. When I pan up down, the view moves left and right. When I pan left/right, the view moves up and down. If I change the code (flip the x and y's) to below, it at least works up down but, then the pan left move it right and the pan right moves it left.

I am dropping the action on the 'whole' view. IE: not just an image view but attaching it to the main view by highlighting the outside of the outline in the .xib file.

Anyone encounter anything strange like this? Happens on both the simulator and the iPad.

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


Quote:
Originally Posted by Scruffy View Post
Finally got it, thanks to a lot of help and a tutorial. Added Pan Gesture Recognizers from the objects library to the image containers on the storyboard, added some code and hooked it up and it works!

The code looks like this:

In ViewController.h ---->

- (IBAction)handlePanUIPanGestureRecognizer *)recognizer;


In ViewController.m ---->


- (IBAction)handlePanUIPanGestureRecognizer *)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];

}

Then you select the UIPanGestureRecongizer in Interface Builder, bring up the Connections inspector, and drag a line from the selector to the View Controller, and choose handlePan and thats it.
Now everything works smoothly, and it rotates too

Thanks again
Scruffy
kmt is offline   Reply With Quote
Reply

Bookmarks

Tags
button, drag, images, refresh, reset

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 398
13 members and 385 guests
bignoggins, BSH, djqbert, Duncan C, epaga, flamingliquid, jbro, jcdevelopments, leighec68, markolo, nobstudio, revg, taylor202
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,645
Threads: 94,111
Posts: 402,862
Top Poster: BrianSlick (7,990)
Welcome to our newest member, leighec68
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:04 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0