멀티뷰에서 드래그

CookBook - sample 소스 참조
==========================



#import <UIKit/UIKit.h>
#include "time.h"

//@class UIImageView;

@interface DragView : UIImageView
{
CGPoint startLocation;
}
@end

@implementation DragView

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;
[[self superview] bringSubviewToFront:self];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint pt = [[touches anyObject] locationInView:self];
CGRect frame = [self frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[self setFrame:frame];
}
@end//end of implementation

@interface HelloController : UIViewController
@end

@implementation HelloController
#define MAXFLOWER 10
CGPoint randomPoint(){
return CGPointMake(random()% 256, random()% 396);
}
- (void)loadView{
UIView *contentView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blackColor];
self.view = contentView;
[contentView release];
// add the flowers to random points on the screen
for (int i=0; i<MAXFLOWER; i++) {
CGRect dragRect = CGRectMake(0.0f, 0.0f, 64.0f, 64.0f);
dragRect.origin = randomPoint();
DragView *dragger = [[DragView alloc] initWithFrame:dragRect];
NSString *whichFlower = [[NSArray arrayWithObjects:@"blueFlower.png",@"pinkFlower.png",@"orangeFlower.png",nil] objectAtIndex:(random()%3)];
[dragger setImage:[UIImage imageNamed:whichFlower]];
[dragger setUserInteractionEnabled:YES];
[self.view addSubview:dragger];
[dragger release];
}
}
@end


@interface SampleAppDelegate : NSObject <UIApplicationDelegate>
{
}
@end

@implementation SampleAppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
srandom(time(0));
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
HelloController *hello = [[HelloController alloc]init];
[window addSubview:hello.view];
[window makeKeyAndVisible];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
//handle any final state matters here
}


- (void)dealloc
{
[super dealloc];
}

@end //end of SampleApplication implementation

int main(int argc, char *argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"SampleAppDelegate");
[pool release];
return retVal;
}