views:

213

answers:

1

Whats a simple way to let the user drag an imageview around to position it in the place of their choice. Its for a photography app.

Thanks!

A: 

First, make a container view that would take care of touch events. Then, you need a UIIMageView subclass that will make sure, that when you set a center of the object, it doesn't go off-the-screen.

I made a class like that some time ago, here's the code (it can also be animatable).

.h

#import <UIKit/UIKit.h>

@interface ObjectView : UIImageView 
{
}

-(void) setCenter: (CGPoint) center inBounds: (CGRect) bounds withAnimation: (BOOL) animate;

@end

.m

#import "ObjectView.h"

@implementation ObjectView

#define MOVE_TO_CENTER_DURATION 0.1
-(void) setCenter: (CGPoint) center inBounds: (CGRect) bounds withAnimation: (BOOL) animate
{
    CGRect frame = self.frame;

    if(center.x + frame.size.width/2 > bounds.origin.x + bounds.size.width) 
        center.x -= ((center.x + frame.size.width/2) - (bounds.origin.x + bounds.size.width));
    else if(center.x - frame.size.width/2 < bounds.origin.x) 
        center.x += bounds.origin.x - (center.x - frame.size.width/2);

    if(center.y + frame.size.height/2 > bounds.origin.y + bounds.size.height) 
        center.y -= (center.y + frame.size.height/2) - (bounds.origin.y + bounds.size.height);
    else if(center.y - frame.size.height/2 < bounds.origin.y) 
        center.y += bounds.origin.y - (center.y - frame.size.height/2);

    if(animate) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration: MOVE_TO_CENTER_DURATION];
    }

    self.center = center;

    if(animate)
        [UIView commitAnimations];

} 

@end

Hope it helps a bit!

tadej5553
Ok..Im kinda confused about what I should put in the arguments since i've never used any CG stuff.
The cneter is the center of the UIImageView. The bounds is the bounds of it's superview, where to put the UIImageView.To get the bounds, just simply use self.bounds. The center should be the position of the touch
tadej5553
The bounds of the super view would be the screen dimensions right? And I would have to detect the users touch and input as the center
Exactly! You got it :)
tadej5553
And one more thing: In the method, that detects touches, make sure that the first time that the user touches the you set the method od this ObjectaView to animatable. Otherwise it will "snap" to the center. If you use animatable, the trasition is very smooth.
tadej5553
alrighty thanks so much!
how would i link my exitsing view to this to make it the container for touch events?
You actually don't have to do anything.Just implement the touch methods, and in them, set the center of the ObjectView using the method I posted.
tadej5553
CGRect frame = self.frame; and self.center = center; give me errors of request for memeber frame and request for member center in something not on a structure or union?im too noob at cocoa to figure that out
Check again if you have copy/pasted correct. The error shouldn't appear, since the ObjectView is a descendadnt of a UIView which has the center and frame properties
tadej5553
can i just copy it into my view controlleror do i need to create a new file
I thought something like that happend :D. This is a delcleration for a class, not just the method. You must use this class instead of the UIImageView! You can still set pictures (even in IB), because it is a descendant of UIImageView.
tadej5553
erhm...what kinda class do i make and how to i assign it to the uiimageview like make it a decentant (sorry for the questions im a noob O_O)
wait. i think i get how you make it a decentant because you made UIImageView the superclass so this class is that parent...right? but what type of file should i put this in? a Obj-C sub class?
All correct. You're making progress. This code should be as a class in XCode.
tadej5553
OK. Just a objective-C class or a uisubview?and I really appreciate all the help your giving me!
It's a UIImageView subclass. Just copy and paste the class that I wrote in my original post.Don't wory, I'll help you till you get this thing working :D
tadej5553
whoo succeeded! I can probally figure out the rest thanks!!