Friday, July 30, 2010

Drawing in iPhone

Now, I am going to explain how to draw in iPhone.


For drawing we can use touch events (touchBegin and touchMoved).
First,create a View Based Application.
And then Add a new class which inherits from uiimageview, name of class be "MyImageView"
After that in MyImageView.h, add a CGPoint to store the starting Point and NSstring to to knoe the brush is for Drawing or Erasing.


MyImageView.h file look like








And in implemation File(MyImageView.m) look like






To Download complete project  Sample Code here

Friday, July 9, 2010

Draggable or Movable Image in iPhone

Hi,
I am going to tell how to create a draggable Image in iphone application


First, create a view-based application and name it as your wish for my application i am creating with name MyDragApp. It creates a MyDragAppAppDelegate.h & .m, MyDragAppViewController.h &.m files.


Now add a new file for moving the image, it should inherits the UIImageView,


DraggableImage.h

@interface DraggableImage : UIImageView {
CGPoint startPoint;
 }

@end


For moving the Image we are going to use the "touchesBegan and touchesMoved" Delegate.

DraggableImage.m
#import "myDraggable.h"



@implementation myDraggable
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// Retrieve the touch point
CGPoint pt = [[touches anyObject] locationInView:self];
startPoint = pt;

}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
// Move relative to the original touch point
CGPoint pt = [[touches anyObject] locationInView:self];
CGRect frame = [self frame];
frame.origin.x += pt.x - startPoint.x;
frame.origin.y += pt.y - startPoint.y;
[self setFrame:frame];
}
@end



After that Create an object for that DraggableImage in Main View and then setUserInteractionEnabled:YES for that object and added to ur view and then run your application to Drag the Image


MyDragAppViewController.m

#import "MyDragAppViewController.h"
#import "DraggableImage.h"

@implementation MyDragAppViewController

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
DraggableImage *dragImage=[[DraggableImage alloc]
  initWithImage:[UIImage imageNamed:@"baby-1.jpg"]];
[dragImage setUserInteractionEnabled:YES];
[self.view addSubview:dragImage];
[dragImage release];
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

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



To Download the Project Click Here