Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

Beispielcode zu Drag’n’Drop

8. Oktober 2010

Hier ist ein Tutorial-Code zum Thema Drag'n'Drop. Das Beispiel (Recipe: Dragging Views) ist aus dem Buch "The iPhone Developer's Cookbook" von Erica Sadun. Zu diesem Buch gibt es hier Codebeispiele.
#import <UIKit/UIKit.h> #define COOKBOOK_PURPLE_COLOR    [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f] @interface DragView : UIImageView { CGPoint startLocation; } @end @implementation DragView - (id) initWithImage: (UIImage *) anImage { if (self = [super initWithImage:anImage]) self.userInteractionEnabled = YES; return self; } - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { // Calculate and store offset, and pop view into front if needed CGPoint pt = [[touches anyObject] locationInView:self]; startLocation = pt; [[self superview] bringSubviewToFront:self]; } - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { // Calculate offset CGPoint pt = [[touches anyObject] locationInView:self]; float dx = pt.x - startLocation.x; float dy = pt.y - startLocation.y; CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy); // Set new location self.center = newcenter; } @end @interface TestBedViewController : UIViewController @end @implementation TestBedViewController #define MAXFLOWERS 12 CGPoint randomPoint() { int half = 32; // half of flower size int freesize = 240 - 2 * half; // inner area return CGPointMake(random() % freesize + half, random() % freesize + half); } - (void) viewDidLoad { self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR; srandom(time(0)); // Add the flowers to random points on the screen for (int i = 0; i < MAXFLOWERS; i++) { NSString *whichFlower = [[NSArray arrayWithObjects:@"blueFlower.png", @"pinkFlower.png", @"orangeFlower.png", nil] objectAtIndex:(random() % 3)]; DragView *dragger = [[DragView alloc] initWithImage:[UIImage imageNamed:whichFlower]]; dragger.center = randomPoint(); [self.view addSubview:dragger]; [dragger release]; } } @end @interface TestBedAppDelegate : NSObject <UIApplicationDelegate> @end @implementation TestBedAppDelegate - (void)applicationDidFinishLaunching:(UIApplication *)application { UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[TestBedViewController alloc] init]]; [window addSubview:nav.view]; [window makeKeyAndVisible]; } @end int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate"); [pool release]; return retVal; }