views:

68

answers:

2

hi i was trying to complete an iphone projct from the iphone developers cookbook on draggable views clipped and i was having difficult working out which segments of code goes into what file iv been messing around awhile with it now any body can give me some direction heres the code

/*
*DragView:  Draggable views
/*

@interface DragView  :UIImageView
{
     cgpiontstartLocation
}
@end


@implementation DragView

// note the touch piont brings the touched view to the front 
-(void) touchesbegan: (NSSet*) touches withevent:  (UIEvent*) event

{
  CGPoint = [[touches anyObject] locationinView: self;
  startlocation = pt;
  [[self superview] bringSubviewTofront:self;
}

//as the user drags move the flower with the touch
- (void) touchesMoved (NSSet*) touches withEvent:(uiEvent*) event
{

   cg*oint pt = [[touches anyObject] locatoininView:self];
   CGRect frame = {self];


   frame.origin.x += pt.x - startLocation.x;
   frame.origin.y += pt.y - startLocation.y;
   [self setFrame:frame]

}
@end

and

/*
*Hello Controler: The primer view controller
*/

@@interface HelloController : UIViewController
{
  UIView *contentview;
}

@end



@implementation HelloContrller

#define MAXFLOWERS 16

CGPiont randomPoint() { return CGPointMake(random() % 256, random() % 396);}

- (void) loadView
{
  //create the main view with a balck backgroung
  CGRect apprect = [[UIScreen mainScreen] applicationFrame];
  contentView = [[UIVIEW alloc] initwithframe:apprect];
  contentView.backgroundColor = [ UIColor blackColor];
  self.View = contentView;
  [contentView release];

  // add the flowers to randompoints on the screen
  for (int 1 = 0; i < MAXFLOWERS; i++)
  CGRect dragRect = CGRectMake (0.0f, 0.0f, 64.0f64.0f);
  dragRect.origin = randomPoint();
  DragView *Dragger [[DragView alloc] initwithFrame:dragRect];
  [dragger setUserInteractionEnable:YES];

  //select random flower
  NSString *whichFlower [[NSArray arrayWithObjects:@"bluedove.png",
    @"purpledove.png", @"reddove.png",nil] objectAtIndex:(random() %
    3)];
  [dragger setImage:[UIImage imageNamed:whichFlower]];

  //add the new subview
  [contentView addSubview:dragger];
  [dragger release];
  }
}

_(viod) dealloc

{
  [contentView release];
  [super dealloc];
}
@end
+1  A: 

I would suggest first going to the Apple Sample code and downloading a sample project. Spend a little time figuring out how it works, change some code and see how it affects the app. This will teach you how to put together an app. If you teach a man how to fish...

coneybeare
+1  A: 

Looks like you'll want to create "DragView.h" and "DragView.m".

The @interface section goes into DragView.h and the @implementation section goes into DragView.m.

In your HelloController, you'll need to add at the top: #include "DragView.h".

Rob