I want my UIImageView to detect both Tap and Swipe gesture. I've coded the touchesBegan method for Swipe detection, I just want to knw how can i make my ImageView detect both the gestures.
how to code the touchesBegan so that it can handle both of them??
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] != 1)
return;
floatSwipeStartX = [[touches anyObject] locationInView:self.view].x;
floatSwipeStartY = [[touches anyObject] locationInView:self.view].y;
intSwipeDirection = 0;
isSwiping = YES;
viewUp.hidden = NO;
viewLeft.hidden = NO;
viewCurrent.hidden = NO;
viewRight.hidden = NO;
viewDown.hidden = NO;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (!isSwiping || [touches count] != 1){
return;
}
CGFloat swipeDistanceX = [[touches anyObject] locationInView:self.view].x - floatSwipeStartX;
CGFloat swipeDistanceY = [[touches anyObject] locationInView:self.view].y - floatSwipeStartY;
CGSize contentSize = [self setContentSize];
if (! intSwipeDirection) {
if (abs(swipeDistanceX) > abs(swipeDistanceY)) { // swipe left or right
intSwipeDirection = SWIPE_DIRECTION_HORIZONTAL;
} else {
intSwipeDirection = SWIPE_DIRECTION_VERTICAL;
}
}
if (intSwipeDirection == SWIPE_DIRECTION_HORIZONTAL) {
viewLeft.frame = CGRectMake(swipeDistanceX - contentSize.width, 0.0f, contentSize.width, contentSize.height);
viewCurrent.frame = CGRectMake(swipeDistanceX, 0.0f, contentSize.width, contentSize.height);
viewRight.frame = CGRectMake(swipeDistanceX + contentSize.width, 0.0f, contentSize.width, contentSize.height);
} else {
viewUp.frame = CGRectMake(0.0f, swipeDistanceY - contentSize.height, contentSize.width, contentSize.height);
viewCurrent.frame = CGRectMake(0.0f, swipeDistanceY, contentSize.width, contentSize.height);
viewDown.frame = CGRectMake(0.0f, swipeDistanceY + contentSize.height, contentSize.width, contentSize.height);
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGFloat swipeDistanceX = [[touches anyObject] locationInView:self.view].x - floatSwipeStartX;
CGFloat swipeDistanceY = [[touches anyObject] locationInView:self.view].y - floatSwipeStartY;
if (! isSwiping || (swipeDistanceX == 0 && swipeDistanceY == 0)) {
//[self updateViews];
return;
}
if (intSwipeDirection == SWIPE_DIRECTION_HORIZONTAL) {
if (swipeDistanceX > 50.0f) {
intNewDirection = SWIPE_TO_THE_RIGHT;
intCurrentView = intCurrentLeft;
} else if (swipeDistanceX < -50.0f) {
intNewDirection = SWIPE_TO_THE_LEFT;
intCurrentView = intCurrentRight;
}
} else { // vertical
if (swipeDistanceY > 50.0f) {
intNewDirection = SWIPE_TO_THE_UP;
intCurrentView = intCurrentUp;
} else if (swipeDistanceY < -50.0f) {
intNewDirection = SWIPE_TO_THE_DOWN;
intCurrentView = intCurrentDown;
}
}
[self updateViews];
isSwiping = NO;
}
Please guide me where to insert the code for Tap gesture. I'm using simulator 3.2
If you're using sdk 3.2 or higher, this is dead easy with the UIGestureRecognizer class.
Otherwise, I'd do something along the lines of checking the amount of movement of the finger between touches began and touches ended, calling the gesture a tap if the movement is minimal and a swipe if it's large.
Related
I want to limit my uiimageview size while using CGAffineTransform. I am limitting scale but I can't limit the size of the my uiimageview. When I run the app I see it is limitted but in the background the size of my uiimageview keeps increasing. How can I make this?
Here is my code:
- (id)initWithFrame:(CGRect)frame
{
if ([super initWithFrame:frame] == nil) {
return nil;
}
originalTransform = CGAffineTransformIdentity;
touchBeginPoints = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = YES;
self.exclusiveTouch = YES;
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSMutableSet *currentTouches = [[[event touchesForView:self] mutableCopy] autorelease];
[currentTouches minusSet:touches];
if ([currentTouches count] > 0) {
[self updateOriginalTransformForTouches:currentTouches];
[self cacheBeginPointForTouches:currentTouches];
}
[super touchesBegan:touches withEvent:event];
[self cacheBeginPointForTouches:touches];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGAffineTransform incrementalTransform = [self incrementalTransformWithTouches:[event touchesForView:self]];
self.transform = CGAffineTransformConcat(originalTransform, incrementalTransform);
CGAffineTransform transform = self.transform;
float scale = sqrt(transform.a*transform.a + transform.c*transform.c);
NSLog(#"%f",self.frame.size.height);
if (scale > SCALE_MAX){
self.transform = CGAffineTransformScale(transform, SCALE_MAX/scale, SCALE_MAX/scale);
}
else if (scale < SCALE_MIN){
self.transform = CGAffineTransformScale(transform, SCALE_MIN/scale, SCALE_MIN/scale);
}
[super touchesMoved:touches withEvent:event];
}
-(void)updateOriginalTransformForTouches:(NSSet *)touches{
CGAffineTransform transform = self.transform;
float scale = sqrt(transform.a*transform.a + transform.c*transform.c);
if (scale > SCALE_MAX){
self.transform = CGAffineTransformScale(transform, SCALE_MAX/scale, SCALE_MAX/scale);
}
else if (scale < SCALE_MIN){
self.transform = CGAffineTransformScale(transform, SCALE_MIN/scale, SCALE_MIN/scale);
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
if (touch.tapCount >= 2) {
[self.superview bringSubviewToFront:self];
}
}
[self updateOriginalTransformForTouches:[event touchesForView:self]];
[self removeTouchesFromCache:touches];
NSMutableSet *remainingTouches = [[[event touchesForView:self] mutableCopy] autorelease];
[remainingTouches minusSet:touches];
[super touchesEnded:touches withEvent:event];
[self cacheBeginPointForTouches:remainingTouches];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
You can know CGRect of UIImagView before applying transform on it like below example :
//Create transform
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1.2, 1.2);
//Apply transform on UIImageView to get CGRect
CRect newRect = CGRectApplyAffineTransform(yourImgView.frame, scaleTransform);
//Check if rect is in valid limitation
if(newRect.size.height > 200 && newRect.size.width >200) //your condition to limit UIImageView's Size
{
//Valid so apply transform
yourImageView.transform = scaleTransform
}
else
{
//rect increases so no transform
// no transform to UIImageView
NSLog(#"%#",NSStringFromCGRect(yourImageView.frame))
}
I was facing the a similar problem, I have an image view that's rotating according to compass heading data and also has to move position according to device change in interface orientation
I created a tempImageView to help maintain frame, and kept reseting it with every change in device orientation
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// equal tempImageView to imageView
_tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-75, 150, 64, 61)];
[_tempImageView setImage:[UIImage imageNamed:#"image.gif"]];
_tempImageView.transform = _imageView.transform;
// reset imageView
[_imageView removeFromSuperview];
_imageView = _tempImageView;
[self.view insertSubview:_imageView atIndex:1];
}
I'm trying to add 10 levels and 1 per page, which is 10 pages. How can I use this code to do that? Right now it only has two pages. Can anyone help?
-(id) init
{
if ((self = [super init]))
{
CGSize s = [[CCDirector sharedDirector] winSize];
self.isTouchEnabled = YES;
isDragging = NO;
lastX = 0.0f;
xVel = 0.0f;
contentWidth = s.width * 10.0;
currentPage = 0;
// main scrolling layer - add as child to this page layer.
scrollLayer = [[[LevelScene alloc] init] autorelease];
scrollLayer.anchorPoint = ccp(0, 1);
scrollLayer.position = ccp(0, 0);
[self addChild:scrollLayer];
[self schedule:#selector(moveTick:) interval:0.02f];
}
return self;
}
- (void) moveTick: (ccTime)dt
{
float friction = 0.99f;
CGSize s = [[CCDirector sharedDirector] winSize];
if (!isDragging)
{
// inertia
xVel *= friction;
CGPoint pos = scrollLayer.position;
pos.x += xVel;
// to stop at bounds
pos.x = MAX(-s.width, pos.x);
pos.x = MIN(0, pos.x);
if (pos.x == -s.width)
{
xVel = 0;
currentPage = 1;
}
if (pos.x == 0)
{
xVel = 0;
currentPage = 0;
}
// snap to page by quickly moving to it: e.g.: xVel = 40
if (fabsf(xVel) < 10)
{
if (pos.x < -s.width/2.0)
{
xVel = -40;
}
else {
xVel = 40;
}
}
scrollLayer.position = pos;
}
else {
xVel = (scrollLayer.position.x - lastX)/2.0;
lastX = scrollLayer.position.x;
}
}
- (void) ccTouchesBegan: (NSSet *)touches withEvent: (UIEvent *)event
{
isDragging = YES;
}
- (void) ccTouchesMoved: (NSSet *)touches withEvent: (UIEvent *)event
{
CGSize s = [[CCDirector sharedDirector] winSize];
UITouch *touch = [touches anyObject];
// simple position update
CGPoint a = [[CCDirector sharedDirector] convertToGL:[touch previousLocationInView:touch.view]];
CGPoint b = [[CCDirector sharedDirector] convertToGL:[touch locationInView:touch.view]];
CGPoint nowPosition = scrollLayer.position;
nowPosition.x += (b.x - a.x);
nowPosition.x = MAX(-s.width, nowPosition.x);
nowPosition.x = MIN(0, nowPosition.x);
scrollLayer.position = nowPosition;
}
- (void) ccTouchesEnded: (NSSet *)touches withEvent: (UIEvent *)event
{
isDragging = NO;
}
Any help is greatly appreciated! Thanks!
Jon, it seems like you're trying to recreate a UIScrollView in cocos2d. If that's the case, might I suggest using an existing open source project known as CCScrollLayer (HERE) Out of the box it should do everything you need to do and is pretty easy to extend to meet your needs better.
I found a very strange problem while handling touch events. The idea of an app is that i have an ImageView which contains a circle with text, that the user can rotate.
I've implemented a custom UIScrollView subclass to contain circle image. There i implemented methods touchesBegan, touchesMoved and touchesEnded in order to rotate my circle when user drags it left or right. Everything works fine, but when you try to drag it with one finger very fast from one side to another and in opposite direction, methods touchesBegan and touchesEnded are called different number of times. For example touchesBegan was called 1 time and touchesEnded 2 - 3 times. How can it be?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if([[event touchesForView:self] count]==1){
touchPoint = [[touches anyObject] locationInView:self];
previousTouchPoint = touchPoint;
angle = 0;
if (((touchPoint.x > 160)&&(touchPoint.y < 210))||((touchPoint.x < 160)&&(touchPoint.y > 210))) {
leftRotation = YES;
}
else {
leftRotation = NO;
}
currentMoveAngle = 0;
}
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{
if([[event touchesForView:self] count] == 1){
CGPoint newPoint = [[touches anyObject] locationInView:self];
CGPoint origin;
if (self.tag == 2) {
origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215);
}
else {
origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215);
}
previousTouchPoint.x -= origin.x;
previousTouchPoint.y -= origin.y;
CGPoint second = newPoint;
second.x -= origin.x;
second.y -= origin.y;
CGFloat rotationAngle = [self rotationFromFirstPoint:previousTouchPoint toSecondPoint:second];
previousTouchPoint = newPoint;
[self rotateContentToAngle:rotationAngle animated:NO];
currentMoveAngle += rotationAngle;
}
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{
if ([[event touchesForView:self] count] == 1){
rotating = YES;
CGFloat rotationAngle;
CGPoint origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215);
CGPoint lastPoint = [[touches anyObject] locationInView:self];
CGPoint touchP = touchPoint;
if ((touchP.x != lastPoint.x)||(touchP.y != lastPoint.y)) {
touchP.x -= origin.x;
touchP.y -= origin.y;
lastPoint.x -= origin.x;
lastPoint.y -= origin.y;
if (fabs(currentMoveAngle)>M_PI/6) {
NSInteger index = (int)trunc(currentMoveAngle/(M_PI/3));
currentMoveAngle-=(M_PI/3)*index;
NSLog(#"rotation index: %i",index);
}
if (leftRotation) {
rotationAngle = M_PI/3;
rotationAngle-=currentMoveAngle;
}
else {
rotationAngle = (-1)*M_PI/3;
rotationAngle-=currentMoveAngle;
}
[self rotateContentToAngle:rotationAngle animated:YES];
}
}
}
UIScrollView handle touch event very badly.... may be it is happening because of dragging of scrollView.
try this
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (!self.dragging){
if([[event touchesForView:self] count]==1){
touchPoint = [[touches anyObject] locationInView:self];
previousTouchPoint = touchPoint;
angle = 0;
if (((touchPoint.x > 160)&&(touchPoint.y < 210))||((touchPoint.x < 160)&&(touchPoint.y > 210))) {
leftRotation = YES;
}
else {
leftRotation = NO;
}
currentMoveAngle = 0;
}
}
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{
if (!self.dragging){
if([[event touchesForView:self] count] == 1){
CGPoint newPoint = [[touches anyObject] locationInView:self];
CGPoint origin;
if (self.tag == 2) {
origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215);
}
else {
origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215);
}
previousTouchPoint.x -= origin.x;
previousTouchPoint.y -= origin.y;
CGPoint second = newPoint;
second.x -= origin.x;
second.y -= origin.y;
CGFloat rotationAngle = [self rotationFromFirstPoint:previousTouchPoint toSecondPoint:second];
previousTouchPoint = newPoint;
[self rotateContentToAngle:rotationAngle animated:NO];
currentMoveAngle += rotationAngle;
}
}
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{
if (!self.dragging){
if ([[event touchesForView:self] count] == 1){
rotating = YES;
CGFloat rotationAngle;
CGPoint origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215);
CGPoint lastPoint = [[touches anyObject] locationInView:self];
CGPoint touchP = touchPoint;
if ((touchP.x != lastPoint.x)||(touchP.y != lastPoint.y)) {
touchP.x -= origin.x;
touchP.y -= origin.y;
lastPoint.x -= origin.x;
lastPoint.y -= origin.y;
if (fabs(currentMoveAngle)>M_PI/6) {
NSInteger index = (int)trunc(currentMoveAngle/(M_PI/3));
currentMoveAngle-=(M_PI/3)*index;
NSLog(#"rotation index: %i",index);
}
if (leftRotation) {
rotationAngle = M_PI/3;
rotationAngle-=currentMoveAngle;
}
else {
rotationAngle = (-1)*M_PI/3;
rotationAngle-=currentMoveAngle;
}
[self rotateContentToAngle:rotationAngle animated:YES];
}
} }
}
or make scrollingEnabled property NO.
Hello I want to make an application in which I have to display pdffile on iphone screen, which has a functionality of zooming. I have multiple pages of pdffile, but the problem is i can get display only one page.
Here is the code :
/*myView.m*/
#implementation MyView
- (void)configureTiledLayer {
if([global getfirsttime] == 0)
{
[global fetchpageCtr : 1];
[global fetchfirsttime:1];
}
zoom = 1.0f;
tiledLayer = [CATiledLayer layer];
TiledDelegate *delegate = [[TiledDelegate alloc] init];
tiledLayer.delegate = delegate;
// get tiledLayer size
CGRect pageRect = CGPDFPageGetBoxRect(delegate.map, kCGPDFCropBox);
int w = pageRect.size.width;
int h = pageRect.size.height;
NSLog(#"height==%d,weight=%d",h,w);
// get level count
int levels = 1;
while (w > 1 && h > 1) {
levels++;
w = w >> 1;
h = h >> 1;
}
NSLog(#"Layer create");
// set the levels of detail
tiledLayer.levelsOfDetail = levels;
// set the bias for how many 'zoom in' levels there are
tiledLayer.levelsOfDetailBias = 5;
// setup the size and position of the tiled layer
CGFloat width = CGRectGetWidth(pageRect);
CGFloat height = CGRectGetHeight(pageRect);
tiledLayer.bounds = CGRectMake(0.0f, 0.0f, width, height);
CGFloat x = width * tiledLayer.anchorPoint.x;
CGFloat y = -height * tiledLayer.anchorPoint.y;
tiledLayer.position = CGPointMake(x * zoom, y * zoom);
tiledLayer.transform = CATransform3DMakeScale(zoom, zoom, 1.0f);
// transform the super layer so things draw 'right side up'
CATransform3D superTransform = CATransform3DMakeTranslation(0.0f, self.bounds.size.height, 0.0f);
self.layer.transform = CATransform3DScale(superTransform, 1.0, -1.0f, 1.0f);
[self.layer addSublayer:tiledLayer];
[tiledLayer setNeedsDisplay];
moving = NO;
NSLog(#"in layer");
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor blueColor];
[self configureTiledLayer];
}
return self;
}
- (id)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
[self configureTiledLayer];
}
return self;
}
- (void)setZoom:(CGFloat)newZoom {
zoom = newZoom;
tiledLayer.transform = CATransform3DMakeScale(zoom, zoom, 1.0f);
}
- (void)zoomIn {
[self setZoom:zoom * 2.0f];
}
- (void)zoomOut {
[self setZoom:zoom * 0.5f];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(touches.count == 1) {
previousPoint = [[touches anyObject] locationInView:self];
} else if(touches.count == 2) {
// pinch zoom
pinchZoom = YES;
NSArray *touches = [event.allTouches allObjects];
CGPoint pointOne = [[touches objectAtIndex:0] locationInView:self];
CGPoint pointTwo = [[touches objectAtIndex:1] locationInView:self];
previousDistance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) +
pow(pointOne.y - pointTwo.y, 2.0f));
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(touches.count == 1) {
CGPoint currentPoint = [[touches anyObject] locationInView:self];
CGPoint delta = CGPointMake(currentPoint.x - previousPoint.x, currentPoint.y - previousPoint.y);
tiledLayer.position = CGPointMake(tiledLayer.position.x + delta.x * zoom,
tiledLayer.position.y + delta.y * zoom);
previousPoint = currentPoint;
moving = YES;
} else if(touches.count == 2) {
// pinch zoom stuff
NSArray *touches = [event.allTouches allObjects];
CGPoint pointOne = [[touches objectAtIndex:0] locationInView:self];
CGPoint pointTwo = [[touches objectAtIndex:1] locationInView:self];
CGFloat distance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) +
pow(pointOne.y - pointTwo.y, 2.0f));
CGFloat newZoom = fabs(zoom + (distance - previousDistance) / previousDistance);
[self setZoom:newZoom];
previousDistance = distance;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(!moving) {
if(touches.count == 1) {
// realy should recenter on a click but I'm being lazy
if([[touches anyObject] tapCount] == 2) {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self zoomOut];
} else {
[self performSelector:#selector(zoomIn) withObject:nil afterDelay:0.25];
}
}
} else {
moving = NO;
}
}
- (void)dealloc {
[tiledLayer release];
[super dealloc];
}
/*TiledDelegate.m*/
#implementation TiledDelegate
- (CGPDFDocumentRef)sfMuni {
if(NULL == sfMuni) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"Hunting-TrappingSynopsis_0910" ofType:#"pdf"];
NSURL *docURL = [NSURL fileURLWithPath:path];
sfMuni = CGPDFDocumentCreateWithURL((CFURLRef)docURL);
}
return sfMuni;
}
- (CGPDFPageRef)map {
int temppageno = [global getpageCtr];
NSLog(#"page ctr ==%d ",temppageno);
if(NULL == map) {
map = CGPDFDocumentGetPage(self.sfMuni, temppageno);
}
return map;
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
NSLog(#"\ndrawLayer:inContext:");
NSLog(#"ctm = %#", NSStringFromCGAffineTransform(CGContextGetCTM(ctx)));
NSLog(#"box = %#\n", NSStringFromCGRect(CGContextGetClipBoundingBox(ctx)));
CGContextDrawPDFPage(ctx, self.map);
}
- (void)dealloc {
CGPDFPageRelease(map);
CGPDFDocumentRelease(sfMuni);
[super dealloc];
}
/*TiledLayerAppDelegate*/
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
/*TiledLayerViewController*/
- (void)viewDidLoad
{
l_pagectr = 2;
UIButton *btn1 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btn1.frame = CGRectMake(0,0,70,50);
[btn1 setBackgroundColor: [UIColor whiteColor]];
btn1.exclusiveTouch = YES;
[btn1 setTitle:#"Next" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(NextPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
-(IBAction)NextPressed : (id)sender
{
[global fetchpageCtr : l_pagectr++];
MyView *myview1=[[MyView alloc]init];
}
#end
Here when I pressed Next button it will have to display me the nest page but will display me the same page.I also get the page referance in "CGPDFPageRef" incremented but not displayed.
Plz help me for this.
Why don't you just use UIWebView? It renders PDF and offers zoom controls. No need to reinvent the wheel. Here is documentation for UIWebView.
I am doing multi touch on UImageView means zoom in and zoom out on image view. I am using followng code but it doesn't work very well. Can anyone look at this code,
#import "ZoomingImageView.h"
#implementation ZoomingImageView
#synthesize zoomed;
#synthesize moved;
define HORIZ_SWIPE_DRAG_MIN 24
define VERT_SWIPE_DRAG_MAX 24
define TAP_MIN_DRAG 10
CGPoint startTouchPosition;
CGFloat initialDistance;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
moved = NO;
zoomed = NO;
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
}
- (void)dealloc {
if([timer isValid])
[timer invalidate];
[super dealloc];
}
- (void) setImage: (UIImage*)img
{
zoomed = NO;
moved = NO;
self.transform = CGAffineTransformIdentity;
[super setImage:img];
}
- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint {
float x = toPoint.x - fromPoint.x;
float y = toPoint.y - fromPoint.y;
return sqrt(x * x + y * y);
}
- (CGFloat)scaleAmount: (CGFloat)delta {
CGFloat pix = sqrt(self.frame.size.width * self.frame.size.height);
CGFloat scale = 1.0 + (delta / pix);
return scale;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if([timer isValid])
[timer invalidate];
moved = NO;
switch ([touches count]) {
case 1:
{
// single touch
UITouch * touch = [touches anyObject];
startTouchPosition = [touch locationInView:self];
initialDistance = -1;
break;
}
default:
{
// multi touch
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
initialDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self]
toPoint:[touch2 locationInView:self]];
break;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
if([timer isValid])
[timer invalidate];
/*if ([touches count] == 1) {
CGPoint pos = [touch1 locationInView:self];
self.transform = CGAffineTransformTranslate(self.transform, pos.x - startTouchPosition.x, pos.y - startTouchPosition.y);
moved = YES;
return;
}****/
if ((initialDistance > 0) && ([touches count] > 1)) {
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
CGFloat currentDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self]
toPoint:[touch2 locationInView:self]];
CGFloat movement = currentDistance - initialDistance;
NSLog(#"Touch moved: %f", movement);
CGFloat scale = [self scaleAmount: movement];
self.transform = CGAffineTransformScale(self.transform, scale, scale);
// }
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
if ([touches count] == 1) {
// double tap to reset to default size
if ([touch1 tapCount] > 1) {
if (zoomed) {
self.transform = CGAffineTransformIdentity;
moved = NO;
zoomed = NO;
}
return;
}
}
else {
// multi-touch
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
CGFloat finalDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self]
toPoint:[touch2 locationInView:self]];
CGFloat movement = finalDistance - initialDistance;
NSLog(#"Final Distance: %f, movement=%f",finalDistance,movement);
if (movement != 0) {
CGFloat scale = [self scaleAmount: movement];
self.transform = CGAffineTransformScale(self.transform, scale, scale);
NSLog(#"Scaling: %f", scale);
zoomed = YES;
}
}
}
- (void)singleTap: (NSTimer*)theTimer {
// must override
}
- (void)animateSwipe: (int) direction {
// must override
}
It is not working fine on device. can anyone tell that where i am wrong.
When you use any CGAffinTransform.... the value of the frame property becomes undefined. In your code you are using the frame.size.width and frame.size.height to calculate the change in size. After the first iteration of CGAffinTransformScale you would not get the right scale factor. According to the documentation bounds would be the right property for scale calculations.