Drawing signature in ipad app and convert it in image - iphone

I am drawing signature in ipad app it works fine but problem is that when it saves image it also saves the border of the signatureView I want to save only the drawing area not complete signatureView here is my code
signatureView=[[UIView alloc] initWithFrame:CGRectMake(100,100,800,500)];
signatureView.backgroundColor=[UIColor colorWithRed:242.0/255.f green:242/255.0f blue:242/255.0f alpha:1];
signatureView.layer.borderWidth =4;
signatureView.layer.borderColor = [UIColor colorWithRed:23.0/255.0f green:190/255.0f blue:210/255.0f alpha:1].CGColor;
signatureView.layer.cornerRadius=30;
[self.view addSubview:signatureView];
UIButton*OkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[OkButton setFrame:CGRectMake(320,448,118,49)];
[OkButton setTitle:#"OK" forState:UIControlStateNormal];
[OkButton setImage:[UIImage imageNamed:#"okT.png"] forState:UIControlStateNormal];
[OkButton addTarget:self action:#selector(onOKButtonClick) forControlEvents:UIControlEventTouchUpInside];
[signatureView addSubview:OkButton];
UILabel*textLabel=[[UILabel alloc] initWithFrame:CGRectMake(20,6,300,50)];
textLabel.font=[UIFont fontWithName:#"Helvetica-Bold" size:16];
textLabel.text=#"Use the touchscreen to sign here";
textLabel.backgroundColor=[UIColor clearColor];
textLabel.textColor=[UIColor grayColor];
[signatureView addSubview:textLabel];
drawScreen=[[MyLineDrawingView alloc]initWithFrame:CGRectMake(10,50,780,400)];
[signatureView addSubview:drawScreen];
[drawScreen release];
//MyLineDrwaingView
#interface MyLineDrawingView : UIView {
UIBezierPath *myPath;
UIColor *brushPattern;
}
#end
#import "MyLineDrawingView.h"
#implementation MyLineDrawingView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor=[UIColor whiteColor];
myPath=[[UIBezierPath alloc]init];
myPath.lineCapStyle=kCGLineCapRound;
myPath.miterLimit=0;
myPath.lineWidth=10;
//brushPattern=[UIColor redColor];
brushPattern=[UIColor blackColor];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
[brushPattern setStroke];
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
-(void)onOKButtonClick {
CGRect rect = [drawScreen bounds]; //use your signature view's Rect means Frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[signatureView.layer renderInContext:context]; //this line is also important for you
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
signImageView.image = img;
}

I have implemented in my code its work for me follow :
.h file add the UIGestureRecognizerDelegate
.m fiel
-(void)scrollGesture:(UIGestureRecognizer *)sender
{
CGPoint touchLocation = [sender locationInView:scrollView];
NSLog(#"%d %d %d %d",touchLocation.y > signatureViewOne.frame.origin.y,touchLocation.y < signatureViewOne.frame.origin.y+80,touchLocation.x > 3, touchLocation.x < 308);
if ((touchLocation.y > signatureViewOne.frame.origin.y && touchLocation.y < signatureViewOne.frame.origin.y+80) &&(touchLocation.x > 3 && touchLocation.x < 308))
{
NSLog(#"Signone found");
scrollView.scrollEnabled = NO;
}
else
if ((touchLocation.y > signatureViewTwo.frame.origin.y && touchLocation.y < signatureViewTwo.frame.origin.y+80) &&(touchLocation.x > 3 && touchLocation.x < 308))
{
NSLog(#"SignTwo found");
scrollView.scrollEnabled = NO;
}
else
{
NSLog(#"not found");
scrollView.scrollEnabled = YES;
sender.enabled = YES;
}
}

This SO topic has exactly the same problem (Drawing and saving of a signature on iOS): iOS: How to convert the self-drawn content of an UIView to an image (widespread general solution returns blank image)?
Please try and mark your question solved, if it helps.

Related

How to create line over UIButtons in ios?

I have 8 UIButtons. Now I want to create line overs these buttons.
I tried and I am able to draw a line. Please check what I am doing.
MyDrawingView.h
#import <UIKit/UIKit.h>
#interface MyDrawingView : UIView{
CGPoint fromPoint;
CGPoint toPoint;
UIColor* currentColor;
UIImage *backgroundImage;
}
#property CGPoint fromPoint;
#property CGPoint toPoint;
#property(nonatomic,retain) UIColor* currentColor;
#end
MyDrawingView.m
#import "MyDrawingView.h"
#implementation MyDrawingView
#synthesize fromPoint;
#synthesize toPoint;
#synthesize currentColor;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
backgroundImage = [UIImage imageNamed:#"office.jpeg"];
}
return self;
}
- (void)drawRect:(CGRect)rect {
//CGPoint drawingTargetPoint = CGPointMake(100,0);
//[backgroundImage drawAtPoint:drawingTargetPoint];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,10);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextMoveToPoint(context,fromPoint.x , fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touchPoint = [touches anyObject];
fromPoint = [touchPoint locationInView:self];
toPoint = [touchPoint locationInView:self];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
toPoint=[touch locationInView:self];
[self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
toPoint = [touch locationInView:self];
[self setNeedsDisplay];
}
Now in another class I am using 8 buttons on viewDidLoad , please check what I am doing.
-(void)viewDidLoad{
8 UIButtons (8 buttons in one line and 10 pixels gap in X-position after every button)
[self method_Call_drawView];
}
-(void)method_Call_drawView{
v = [[MyDrawingView alloc]initWithFrame:CGRectMake(100,350,400,400)];
v.backgroundColor = [UIColor whiteColor];
[v setNeedsDisplay];
[self.view addSubview:v];
}
.h file
#import <UIKit/UIKit.h>
#class MyDrawingView;
#interface iPad_Level1 : UIViewController{
UIButton *btn1;
UIButton *btn2;
UIButton *btn3;
UIButton *btn4;
UIButton *btn5;
UIButton *btn6;
UIButton *btn7;
UIButton *btn8;
MyDrawingView *v;
}
#property(nonatomic,retain)MyDrawingView *v;
#end
I am able to create view and draw a line on a view but I am not able to touch UIButtons and create a line over it.
My line is increasing and decreasing wherever I click but I want this line on my UIButton.
please check what I am trying to do itunes.apple.com/in/app/word-search-puzzles/id609067187?mt=8, check yellow and blue line on the buttons.
Any idea or suggestion would be highly welcome.
You ca add a 1px height sub view before the button you want.
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 1)]; // change the 200 to fit your screen
lineView.backgroundColor = [UIColor blackColor];
[self.view addSubview:lineView];
And for moving this line when user click a button use:
[UIView animateWithDuration:0.5
delay:0.5
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = lineView .frame;
frame.origin.y = 100; // for move
frame.size.width = 50; // for re-size
lineView .frame = frame;
}
completion:^(BOOL finished)
{
NSLog(#"Completed");
}
];
#import <QuartzCore/CAGradientLayer.h>
Import this and add quartzcore framework in the project
-(IBAction)btnTapped:(id)sender{
UIButton *m_btnTemp=((UIButton *)sender);
m_btnTemp.layer.borderColor=[UIColor blackColor];
m_btnTemp.layer.borderWidth= 1.0f;
}
When you want to remove the bordercolor set borderWidth=0;

How to achieve continuous drag drop menu effect?

I'm trying to achieve a Drag and Drop menu affect. I'm not sure how to go about this, perhaps someone has experience with this exact effect.
Quite simply, when a user touches down on a menu item, I want a graphic to appear at their touch location. Their touch will now control the panning of the graphic. Upon releasing the touch, the graphic will sit in its place and assume full alpha.
I'm already familiar with creating pan gestures and instantiating a graphic. So far, I can create the graphic where the menu item is touched. The biggest issue is how I "pass over" the touch gesture so it is a single and continuous motion.
Also, should the menu item be UIButton or UIImageView?
Any help appreciated. Thanks
I had some fun with this one. The following code will grab the image from the button when touched, drag that image at alpha=0.5, and drop it wherever your touches end at alpha=1.0. It will continue to be draggable thereafter.
After importing QuartzCore, create a new file. The .h should read:
#import <Foundation/Foundation.h>
#import <QuartzCore/CAGradientLayer.h>
#import <QuartzCore/CALayer.h>
#interface DraggableImage : CAGradientLayer
- (void)draw:(UIImage *)image;
- (void)moveToFront;
- (void)appearDraggable;
- (void)appearNormal;
#end
and the .m should read:
#import "DraggableImage.h"
#implementation DraggableImage
- (void)draw:(UIImage *)image{
CGRect buttonFrame = self.bounds;
int buttonWidth = buttonFrame.size.width;
int buttonHeight = buttonFrame.size.height;
UIGraphicsBeginImageContext( CGSizeMake(buttonWidth, buttonHeight) );
[image drawInRect:self.bounds];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[newImage drawInRect:self.bounds];
}
- (void)moveToFront {
CALayer *superlayer = self.superlayer;
[self removeFromSuperlayer];
[superlayer addSublayer:self];
}
- (void)appearDraggable {
self.opacity = 0.5;
}
- (void)appearNormal {
self.opacity = 1.0;
}
#end
Now in your main view controller, add:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "DraggableImage.h"
#interface YourViewController : UIViewController{
DraggableImage *heldImage;
DraggableImage *imageForFrame[5]; // or however many
UIButton *buttonPressed;
int imageCount;
}
#property (weak, nonatomic) IBOutlet UIButton *imageButton;
-(IBAction)buildImageLayerForButton:(UIButton *)sender;
- (void)moveHeldImageToPoint:(CGPoint)location;
- (CALayer *)layerForTouch:(UITouch *)touch;
The imageButton in this case would be your apple Button. Now in your .m file, add this:
#synthesize imageButton;
#pragma - mark Touches
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CALayer *hitLayer = [self layerForTouch:[touches anyObject]];
if ([hitLayer isKindOfClass:[DraggableImage class]]) {
DraggableImage *image = (DraggableImage *)hitLayer;
heldImage = image;
[heldImage moveToFront];
}
hitLayer = nil;
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if (heldImage)
{
UITouch *touch = [touches anyObject];
UIView *view = self.view;
CGPoint location = [touch locationInView:view];
[self moveHeldImageToPoint:location];
}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if (heldImage) {
[heldImage appearNormal];
heldImage = nil;
}
}
- (void)dragBegan:(UIControl *)c withEvent:ev {
}
- (void)dragMoving:(UIControl *)c withEvent:ev {
UITouch *touch = [[ev allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
[self moveHeldImageToPoint:touchPoint];
}
- (void)dragEnded:(UIControl *)c withEvent:ev {
UITouch *touch = [[ev allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
[self moveHeldImageToPoint:touchPoint];
[heldImage appearNormal];
heldImage = nil;
}
-(IBAction)buildImageLayerForButton:(UIButton *)sender{
DraggableImage *image = [[DraggableImage alloc] init];
buttonPressed = sender;
CGRect buttonFrame = sender.bounds;
int buttonWidth = buttonFrame.size.width;
int buttonHeight = buttonFrame.size.height;
image.frame = CGRectMake(120, 24, buttonWidth*3, buttonHeight*3);
image.backgroundColor = [UIColor lightGrayColor].CGColor;
image.delegate = self;
imageForFrame[imageCount] = image;
[self.view.layer addSublayer:image];
[image setNeedsDisplay];
[image moveToFront];
[image appearDraggable];
heldImage = image;
[self moveHeldImageToPoint:sender.center];
imageCount++;
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
UIGraphicsPushContext(ctx);
DraggableImage *image = (DraggableImage *)layer;
[image draw:[buttonPressed imageForState:UIControlStateNormal]];
UIGraphicsPopContext();
}
- (void)moveHeldImageToPoint:(CGPoint)location
{
float dx = location.x;
float dy = location.y;
CGPoint newPosition = CGPointMake(dx, dy);
[CATransaction begin];
[CATransaction setDisableActions:TRUE];
heldImage.position = newPosition;
[CATransaction commit];
}
- (CALayer *)layerForTouch:(UITouch *)touch
{
UIView *view = self.view;
CGPoint location = [touch locationInView:view];
location = [view convertPoint:location toView:nil];
CALayer *hitPresentationLayer = [view.layer.presentationLayer hitTest:location];
if (hitPresentationLayer)
{
return hitPresentationLayer.modelLayer;
}
return nil;
}
-(void)viewDidLoad{
[imageButton addTarget:self action:#selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown];
[imageButton addTarget:self action:#selector(dragMoving:withEvent:) forControlEvents: UIControlEventTouchDragInside | UIControlEventTouchDragOutside];
[imageButton addTarget:self action:#selector(dragEnded:withEvent:) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
[super viewDidLoad];
}
- (void)viewDidUnload {
[self setImageButton:nil];
[super viewDidUnload];
}
Et voila! Connect your button, set its image, and throw copies all over the screen. :)
Note: I didn't comment much, but would be happy to answer any questions.
Cheers!
EDIT: fixed the -(void)draw:(UIImage *)image{} so that it would resize the image properly.
if what you want is to pass the touch function to the second graphic (the big one) i think you can do something like this
on .h you have to declare the images that you're going to drag and float variable to remember previous point of the dragable button (i'm assuming you use IOS 5 SDK)
#property(nonatomic, strong) UIImageView* myImage;
#property float pointX;
#property float pointY;
then, at .m you can do this
myImage = [[UIImageView alloc]initWithImage:#"appleImage.jpg"];
myImage.alpha = 0;
//default UIImageView interaction is disabled, so lets enabled it first
myImage.userInteractionEnabled = YES;
[button addTarget:self action:#selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
and then make the drag function
- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
self.myImage.alpha = 0.5;
UITouch *touch = [[event touchesForView:button] anyObject];
CGPoint previousLocation = [touch previousLocationInView:button];
CGPoint location = [touch locationInView:button];
CGFloat delta_x = location.x - previousLocation.x;
CGFloat delta_y = location.y - previousLocation.y;
// move button, to keep the dragging effect
button.center = CGPointMake(button.center.x + delta_x,
button.center.y + delta_y);
// moving the image
button.center = CGPointMake(button.center.x + delta_x,
button.center.y + delta_y);
self.pointX = previousLocation.x;
self.pointY = previousLocation.y;
[button addTarget:self action:#selector(dragRelease:withEvent:) forControlEvents:UIControlEventTouchUpInside];
}
finally, make the dragRelease function where you return the button to its original place and set the alpha of the images to 1
-(void)dragRelease:(UIButton *)button withEvent:(UIEvent *)event
{
self.myImage.alpha = 1;
button.center = CGPointMake(pointX, pointY);
}
and you're done :3
this is just the basic idea though, maybe this isn't what you want, but i hope this helps
edit* : oh and don't forget to synthesize all the properties, also if you're using SDK below 5.0, you can change the "strong" property to "retain"

How to drag mutiple images from horizontal scrollview?

i am making an application were user can be able to select image from horizontal scrollview and one's the image is selected he should also be able to drag all the selected images?
i am able to select one image at a time and able to drag the single image but i want mutiple images to be select one after the other should be able to darg them one by one?
This my code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
imagesName = [[NSArray alloc]initWithObjects:#"hat3.png",#"hat4-1.png",#"t-shirt.png",#"t-shirt.png",
#"Untitled-2.png",#"logo1.jpg",#"logo2.jpg",#"logo3.jpg",nil];
images = [[NSMutableArray alloc]init];
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setMultipleTouchEnabled:YES];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"t-shirt.png"]];
imageView.center = self.view.center;
[self.view addSubview:imageView];
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80);
int xOffset = 0;
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];
//for(int index=0; index < [imagesName count]; index++)
for (int index=0; index < [imagesName count]; index++)
{
UIImageView *img = [[UIImageView alloc] init];
//img.bounds = CGRectMake(10, 10, 50, 50);a
img.bounds = CGRectMake(0, 0, 20, 20);
//img.frame = CGRectMake(5+xOffset, 0, 160, 110);
img.frame = CGRectMake(0+xOffset, 0, 60, 61);
//img.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);
NSLog(#"image: %#",[imagesName objectAtIndex:index]);
img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
[images insertObject:img atIndex:index];
scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[scrollView addSubview:[images objectAtIndex:index]];
xOffset += 170;
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [[event allTouches] anyObject];
for(int index=0;index<[images count];index++)
{
UIImageView *imgView = [images objectAtIndex:index];
NSLog(#"x=%f,y=%f,width =%f,height=%f",imgView.frame.origin.x,imgView.frame.origin.y,imgView.frame.size.width,imgView.frame.size.height);
NSLog(#"x= %f,y=%f",[touch locationInView:self.view].x,[touch locationInView:self.view].y) ;
if(CGRectContainsPoint([imgView frame], [touch locationInView:scrollView]))
{
[self ShowDetailView:imgView];
break;
}
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesMoved:touches withEvent:event];
NSArray *allTouches = [touches allObjects];
int count = [allTouches count];
if (count == 1) {
if (CGRectContainsPoint([imageView frame], [[allTouches objectAtIndex:0] locationInView:self.view])) {
imageView.center = [[allTouches objectAtIndex:0] locationInView:self.view];
return;
}
}
}
please help
suggest me some tutorial
I think a good idea would be to bind all selected images into an NSMutableArray that maintains a reference to them - Add / remove them as they are being selected / deselected respectively. Then, in your "touchesMoved", instead of updating only the UIImageView receiving the touch, iterate over all the selected image views and change their centers as well. Hope this helps!

Adding vertical alphabets selector in UIView

Hi
Can I add the vertical alphabets selector thing like that we use in UITableView, in UIView?
Best Regards
Yes.
If you create the UIView yourself you can do whatever you want.
It's not even that hard in your case. Some UILabels as subviews and some logic in touchesDidSomething:withEvent: to figure out which label is near the touch.
And a delegate method that tells which section was touched.
I think I could need something like that, so I decided to try it.
//myIndexSelectorView.m
- (id)initWithFrame:(CGRect)frame andLabels:(NSArray *)l {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor lightGrayColor];
self.layer.cornerRadius = frame.size.width/2;
labels = [l retain];
NSInteger count;
for (NSString *string in labels) {
CGFloat margin = 5.0f;
CGFloat yPosition = count*(frame.size.height/[labels count]);
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(margin, yPosition, frame.size.width-2*margin, frame.size.height/[labels count])] autorelease];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor darkGrayColor];
label.textAlignment = UITextAlignmentCenter;
label.text = string;
[self addSubview:label];
count++;
}
}
return self;
}
- (void)touch:(UITouch *)touch {
CGPoint touchPosition = [touch locationInView:self];
NSInteger index = touchPosition.y / (self.bounds.size.height / [labels count]);
NSLog(#"Touched: %#", [labels objectAtIndex:index]);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
[self touch:touch];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
[self touch:touch];
}
- (void)dealloc {
[labels release];
[super dealloc];
}
works as expected and looks similar to the index selector of uitableview
as usual, I didn't check for bugs, and this should not be a copy&paste solution.
You can use different UILabels, and set tags to them. Now to detect touches on appropriate label, you can use UITapGestureRecognizer classes
If you are talking about keypad, then that cannot be positioned other than the default position with a reason that apple does not allow to change its position.Hope that help you.Thanks.

Move an UIImageView around in a UIScrollView

I'm creating an application where I want to let the user move (not pan) an UIImageView around by dragging it on the screen. Additionally, I want the user to be able to zoom the UIImageView in and out.
As such I've been using a custom UIScrollView that forwards single touches to the 'contentView':
#implementation JM_UIScrollView
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
NSSet *allTouches = [event allTouches];
NSLog(#"Checking for touches: %d", [allTouches count]);
if ([allTouches count] == 1) {
return YES;
}
return NO;
}
#end
Along with a custom UIImageView that implements touchesBegan and touchesMoved to determine where to move the UIImageView:
#implementation JM_UIImageView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
if ([allTouches count] == 0)
return;
UITouch *firstTouch = [[allTouches allObjects] objectAtIndex: 0];
CGPoint touchLoc = [firstTouch locationInView: [self superview]];
touchOffset= CGPointMake(touchLoc.x-self.center.x,touchLoc.y-self.center.y);
NSLog(#"Currently at: %3.3f x %3.3f", touchLoc.x, touchLoc.y);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
if ([allTouches count] == 0)
return;
UITouch *firstTouch = [[allTouches allObjects] objectAtIndex: 0];
CGPoint touchLoc = [firstTouch locationInView: [self superview]];
if ([allTouches count] == 1)
{
if ([firstTouch view] == self)
{
touchLoc.x -= touchOffset.x;
touchLoc.y -= touchOffset.y;
NSLog(#"Moved to: %3.3f x %3.3f", touchLoc.x, touchLoc.y);
self.center = touchLoc;
}
}
}
#end
This is then all glued together:
scrollView = [[JM_UIScrollView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
scrollView.delegate = self;
scrollView.bouncesZoom = YES;
scrollView.scrollEnabled = NO;
scrollView.backgroundColor = [UIColor redColor];
scrollView.clipsToBounds = YES;
UIImage *image = [UIImage imageNamed:#"berg.jpg"];
imageView = [[JM_UIImageView alloc] initWithFrame: CGRectMake(0, 0, 140, 230)];
imageView.image = image;
imageView.center = CGPointMake(200,300);
[imageView setUserInteractionEnabled: YES];
[scrollView addSubview: imageView];
scrollView.contentSize = CGSizeMake(140,230);
scrollView.minimumZoomScale = 0.2;
scrollView.maximumZoomScale = 1.1;
[window addSubview: scrollView];
// Override point for customization after application launch
[window makeKeyAndVisible];
The problem:
When I start the application, I can move the UIImageView just fine and fluently. I can zoom-in and still be able to move it around.
However, it seems that whenever I zoom back out to the maximum level I seem then unable to move the UIImageView around anymore. It will jump at times, but by a maximum of 10 pixels. Use of NSLog() shows the touchesBegan/touchesMoved methods on the JM_UIImageView are no longer called.
Does anyone have any idea on what I might be missing here?
EDIT:
Would also accept an answer for wether or not this is the only way of implementing pinch-zooming with the zoombounce animation.