Question

I am trying to get pinch/zoom work but to no luck... please check code attached any help is appreciated.

#import "SlideShowViewController.h"
#import "SomAppDelegate.h"
#import "MainCategory.h"
#import <sqlite3.h>
#import "UIImage+Resizing.h"
NSInteger Val;
NSInteger AIX;
NSInteger AIY;
NSInteger Height;
NSInteger Pre;
NSInteger Valtab;
NSInteger ImageVal;
NSInteger ValScroll;
UIToolbar *tabBar;
UIActivityIndicatorView *activityIndicator;
UIActivityIndicatorView *activityIndicator1;

@class SomAppDelegate;
@class SlideShowViewController;
@interface SlideShowView : UIView<UIScrollViewDelegate>
{
 NSArray * mImages;
 UIImageView * mLeftImageView;
 UIImageView * mCurrentImageView;
 UIImageView * mRightImageView;
 NSUInteger mCurrentImage;
 BOOL mSwiping;
 UIImageView * result;
 NSString *imageName;
 CGFloat mSwipeStart;
 UIScrollView *NewView;
 NSTimer    *timer;

//////////////////////////////////////////////////////for pinch and Zoom//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 CGPoint gestureStartPoint;
 CGPoint wmsImageViewCenter;
 double unitDistance;
 double initialDistance;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

}
@property (nonatomic, retain)UIView *NewView;
- (double)getDistance:(CGPoint)fromPoint toPoint:(CGPoint)otherPoint;
-(id)initWithImages:(NSArray *)inImages;


@end 



@interface SlideShowView(PrivateMethods)
-(UIImageView *)newPieceViewWithImageNamed:(NSString *)imageName atPostion:(CGPoint)centerPoint;
- (void)animateFirstTouch:(double)scale;
@end

#pragma mark -
@implementation SlideShowView
@synthesize NewView;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (UIImageView *)createImageView:(NSUInteger)inImageIndex
{
 if (inImageIndex >= [mImages count])
 {
  return nil;
 }

 UIImage *pigImage1 =[UIImage imageWithContentsOfFile:[mImages objectAtIndex:inImageIndex]]; 
 result=[[UIImageView alloc] initWithImage:pigImage1];
    pigImage1=nil;
    [pigImage1 release];
 result.opaque = YES;
 CGRect frameRect = [result frame];
 unitDistance = [self getDistance:CGPointMake(0, 0) toPoint:CGPointMake(frameRect.size.width, frameRect.size.height)] / 4.0;
 result.userInteractionEnabled =NO;
 result.alpha = 1.0;
 result.backgroundColor = [UIColor blackColor];
 result.contentMode = UIViewContentModeScaleAspectFit;
 result.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
 return result;

}

-(UIImage*)createImage:(NSUInteger)inImageIndex
{
 if (inImageIndex >= [mImages count])
 {
  return nil;
 } 

 UIImage *pigImage1 =[UIImage imageWithContentsOfFile:[mImages objectAtIndex:inImageIndex]]; 
 return pigImage1;

}

- (id)initWithImages:(NSArray *)inImages
{

 initialDistance = 1;
 unitDistance = 1;
 [self setMultipleTouchEnabled:YES];
  self.multipleTouchEnabled = YES;


 if (self = [super initWithFrame:CGRectZero])
 {
  mImages = [inImages retain];

  NSUInteger imageCount=[inImages count];
             ImageVal=imageCount;
  if (imageCount > 0)
  {
   mCurrentImageView = [self createImageView:0];
   [self addSubview:mCurrentImageView];
   result=nil;

   if (imageCount > 1)
   {
    mRightImageView = [self createImageView:1];
    [self addSubview:mRightImageView];
    result=nil;
   }

  }

  self.opaque = YES;
  self.backgroundColor = [UIColor blackColor];
  self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

 }
 return self;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (void)dealloc
{
 [mImages release];
 [result release];
 result.image=nil;
 result=nil;
 [super dealloc];
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (void)layoutSubviews
{
 if (mSwiping)
  return;

 CGSize contentSize =self.frame.size;               //self.frame.size;
 mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
 mCurrentImageView.frame = CGRectMake(0.0f,0.0f, contentSize.width, contentSize.height);
 mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ 
//////////////////////////////////////////// code by pinch and zoom //////////////////////////////////////////////////////////////// 

 if (mCurrentImageView != nil)
 {
  if ([touches count] > 1) {
   NSArray *touchArray = [touches allObjects];
   CGPoint p1 = [[touchArray objectAtIndex:0] locationInView:self];
   CGPoint p2 = [[touchArray objectAtIndex:1] locationInView:self];
   initialDistance = [self getDistance:p1 toPoint:p2];
  }
  else
  {

   UITouch *touch = [touches anyObject];
   gestureStartPoint = [touch locationInView:self];
   wmsImageViewCenter = mCurrentImageView.center;

  }

 }

//////////////////////////////////////// code by rajeev ////////////////////////////////////////////////////////////////

 if ([touches count] != 1)
  return;
 mSwipeStart = [[touches anyObject] locationInView:self].x;
 mSwiping = YES;
 mLeftImageView.hidden = NO;
 mCurrentImageView.hidden = NO;
 mRightImageView.hidden = NO;

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 CGSize contentSize =self.frame.size;

 ///////////////////////////////////////////////////for pinch and zoom///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

 if (mCurrentImageView != nil)
 {
  if ([touches count] > 1) 
  {

   NSArray *touchArray = [touches allObjects];
   CGPoint p1 = [[touchArray objectAtIndex:0] locationInView:self];
   CGPoint p2 = [[touchArray objectAtIndex:1] locationInView:self];
   float distanceNew = [self getDistance:p1 toPoint:p2];
   float dx = (distanceNew - initialDistance)/unitDistance;
   double scale = fabs(mCurrentImageView.transform.a + dx);
   [self animateFirstTouch:scale];
   initialDistance = distanceNew;
  }
  else if([touches count]==1)
   {

    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self];
    CGFloat deltaX = gestureStartPoint.x - currentPosition.x;
    CGFloat deltaY = gestureStartPoint.y - currentPosition.y;
    mCurrentImageView.center = CGPointMake(wmsImageViewCenter.x - deltaX, wmsImageViewCenter.y - deltaY);
    mCurrentImageView.opaque = YES;
    mCurrentImageView.contentMode = UIViewContentModeScaleAspectFit;
    mCurrentImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  }
 }



 //////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 CGRect frameRect = [mCurrentImageView frame];
 if(frameRect.size.height>Height)
 {
  contentSize.width=frameRect.size.width;
  contentSize.height= frameRect.size.height;
  mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
  mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
 }
 else 
 {
  if (! mSwiping || [touches count] != 1)
   return;

  CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
  mLeftImageView.frame = CGRectMake(swipeDistance - contentSize.width, 0.0f, contentSize.width, contentSize.height);
  mCurrentImageView.frame = CGRectMake(swipeDistance,0.0f, contentSize.width, contentSize.height);
  mRightImageView.frame = CGRectMake(swipeDistance + contentSize.width, 0.0f, contentSize.width, contentSize.height);

 }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

 if (! mSwiping)
  return;

 CGSize contentSize = self.frame.size;
 NSUInteger count = [mImages count];
 CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
 CGRect frameRect = [mCurrentImageView frame];
 if(frameRect.size.height>Height)
   {
  if (mCurrentImage > 0 && swipeDistance > 250.0f)
  {
   [mRightImageView removeFromSuperview];
   mRightImageView.image=nil;
   mRightImageView=nil;
   result.image=nil;
   result=nil;


   mRightImageView = mCurrentImageView;
   mCurrentImageView = mLeftImageView;
   mCurrentImage--;

   if (mCurrentImage > 0)
   {
    mLeftImageView = [self createImageView:mCurrentImage - 1];
    mLeftImageView.hidden = YES;
    [self addSubview:mLeftImageView];
    result=nil;
   }
   else
   {
    mLeftImageView = nil;
    result=nil;
   }

   [UIView beginAnimations:@"swipe" context:NULL];
   [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
      [UIView setAnimationDuration:0.3f];
   CGSize contentSize = self.frame.size;
   mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
   mCurrentImageView.frame = CGRectMake(0.0f,0.0f, contentSize.width, contentSize.height);
   mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
   [UIView commitAnimations];
   mSwiping = NO;

  }
  //////////////////////////////////////////////////////////right Swaping /////////////////////////////////////////////////////////////////////// 
  else if (mCurrentImage < count - 1 && swipeDistance < -250.0f)
  {
   [mLeftImageView removeFromSuperview];
   mLeftImageView.image=nil;
   mLeftImageView=nil;
   result.image=nil;
   result=nil;
   mLeftImageView = mCurrentImageView;
   mCurrentImageView = mRightImageView;
   mCurrentImage++;

   if (mCurrentImage < count - 1)
   {   
    mRightImageView = [self createImageView:mCurrentImage + 1];
    mRightImageView.hidden = YES;
    [self addSubview:mRightImageView];
    result=nil;
   }
   else
   {  
    mRightImageView=nil;
    result=nil;
   }
   [UIView beginAnimations:@"swipe" context:NULL];
   [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
   [UIView setAnimationDuration:0.3f];
   CGSize contentSize = self.frame.size;
   mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
   mCurrentImageView.frame = CGRectMake(0.0f,0.0f, contentSize.width, contentSize.height);
   mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
   [UIView commitAnimations];
   mSwiping = NO;
  }

 }
 else
 {
  ///////////////////////////////////////////////////// Left swaping//////////////////////////////////////////////////////////////////////////// 

  if (mCurrentImage > 0 && swipeDistance > 50.0f)
  {
   [mRightImageView removeFromSuperview];
   mRightImageView.image=nil;
   mRightImageView=nil;
   result.image=nil;
   result=nil;


   mRightImageView = mCurrentImageView;
   mCurrentImageView = mLeftImageView;
   mCurrentImage--;

   if (mCurrentImage > 0)
   {
    mLeftImageView = [self createImageView:mCurrentImage - 1];
    mLeftImageView.hidden = YES;
    [self addSubview:mLeftImageView];
    result=nil;
   }
   else
   {
    mLeftImageView = nil;
    result=nil;
   }
  }
  //////////////////////////////////////////////////////////right Swaping /////////////////////////////////////////////////////////////////////// 
  else if (mCurrentImage < count - 1 && swipeDistance < -50.0f)
  {
   [mLeftImageView removeFromSuperview];
   mLeftImageView.image=nil;
   mLeftImageView=nil;
   result.image=nil;
   result=nil;

   mLeftImageView = mCurrentImageView;
   mCurrentImageView = mRightImageView;
   mCurrentImage++;

   if (mCurrentImage < count - 1)
   {   
    mRightImageView = [self createImageView:mCurrentImage + 1];
    mRightImageView.hidden = YES;
    [self addSubview:mRightImageView];
    result=nil;
   }
   else
   {  
    mRightImageView=nil;
    result=nil;
   }
  }

  [UIView beginAnimations:@"swipe" context:NULL];
  [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
  [UIView setAnimationDuration:0.3f];
  mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
  mCurrentImageView.frame = CGRectMake(0.0f,0.0f, contentSize.width, contentSize.height);
  mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
  [UIView commitAnimations];
  mSwiping = NO;

 }

}
///////////////////////////////////////////////////////////////////////////////////Tab Bar functionality /////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


#pragma mark -
#pragma mark === Animating subviews ===
#pragma mark

- (void)animateFirstTouch:(double)scale
{
 if (mCurrentImageView != nil)
 {
  CGAffineTransform transform = CGAffineTransformMakeScale(scale, scale);
  mCurrentImageView.transform = transform;

  /*
  CGSize contentSize = self.frame.size;
  if(contentSize.width>1024)
  {
  //contentSize= CGSizeMake(1024, 1024);
  mCurrentImageView.opaque = YES;
  mCurrentImageView.contentMode = UIViewContentModeScaleAspectFit;
  mCurrentImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  mCurrentImageView.clipsToBounds = YES;
  //mCurrentImageView.frame = self.bounds;
  [mCurrentImageView setNeedsLayout];
  }
  */
 }
}

- (double)getDistance:(CGPoint)fromPoint toPoint:(CGPoint)otherPoint
{
 double deltaX = otherPoint.x - fromPoint.x;
 double deltaY = otherPoint.y - fromPoint.y;
 return sqrt(pow(deltaX, 2) + pow(deltaY, 2));
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@end 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SlideShowView

@implementation SlideShowViewController

@synthesize Values,PreVal;
@synthesize ImageID,ImagePath,ImagePos,CatID,SlideID,CID;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
 // Custom initialization
 }
 return self;
 }
 */


/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */

- (void)viewDidLoad 
{ 
 [super viewDidLoad];
 self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
}



- (void)viewWillDisappear:(BOOL)animated 
{ 
 [super viewWillDisappear:animated];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
 int counter=0;
 ImageID=[[NSMutableArray alloc]init];
 ImagePath=[[NSMutableArray alloc]init];
 ImagePos=[[NSMutableArray alloc] init];
 CatID=[[NSMutableArray alloc] init];
 SlideID=[[NSMutableArray alloc] init];
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Category.sqlite3"];
 sqlite3 *db;
 if (sqlite3_open([writablePath UTF8String], &db) == SQLITE_OK)
 {
     sqlite3_stmt *statement = nil;
  NSString *insertQuery=[NSString stringWithFormat:@"select *from Image where SlideID=%d and CatID=%d;",Values,CID];
  const char *sql = [insertQuery cStringUsingEncoding:NSASCIIStringEncoding];
  if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL) != SQLITE_OK)
  {
   NSAssert1(0,@"error1",sqlite3_errmsg(db));
  }
  else
  {
   while(sqlite3_step(statement) == SQLITE_ROW) 
   {
    counter++;
    [ImageID addObject:[NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement,0)]]; 
    [ImagePath addObject:[NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement,1)]];
    [ImagePos addObject:[NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement,2)]];
    [CatID addObject:[NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement,3)]];
    [SlideID addObject:[NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement,4)]];
   }
   sqlite3_finalize(statement);
  }
 }
 sqlite3_close(db);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 Slide= [[[SlideShowView alloc] initWithImages:ImagePath] autorelease];
 self.view=Slide;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
 tabBar.alpha = 0.0;
 if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
 {    AIX=510;
  AIY=360;
  Valtab=690;
  ValScroll=730.0;
  Height=1008;
 }
 else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
 {   
  AIX=510;
  AIY=360;
  Valtab=690;
  ValScroll=730.0;
  Height=750;
 }
 else if(interfaceOrientation == UIInterfaceOrientationPortrait)
 { 
  AIX=360;
  AIY=510;
  Valtab=945;
  ValScroll=530;
  Height=1008;
 }  
 else 
 {   
  AIX=360;
  AIY=510;
  Valtab=945;
  ValScroll=530.0;
  Height=750;
 }
 return YES; //(interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}


- (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 {
    [super viewDidUnload];
    // Release any retained subviews of the main view.

}


- (void)dealloc {
    [super dealloc];
 [activityIndicator release];
}


@end
Was it helpful?

Solution

Try rearranging your init method a little:

- (id)initWithImages:(NSArray *)inImages 
{ 

 initialDistance = 1; 
 unitDistance = 1; 

// remove these
// [self setMultipleTouchEnabled:YES]; 
// self.multipleTouchEnabled = YES; 

 if (self = [super initWithFrame:CGRectZero]) 
 { 
  mImages = [inImages retain]; 

  NSUInteger imageCount=[inImages count]; 
             ImageVal=imageCount; 
  if (imageCount > 0) 
  { 
   mCurrentImageView = [self createImageView:0]; 
   [self addSubview:mCurrentImageView]; 
   result=nil; 

   if (imageCount > 1) 
   { 
    mRightImageView = [self createImageView:1]; 
    [self addSubview:mRightImageView]; 
    result=nil; 
   } 

  } 

  self.opaque = YES; 
  self.backgroundColor = [UIColor blackColor]; 
  self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

  // move to here after the init on super is done
  self.multipleTouchEnabled = YES;
 } 
 return self; 
}

Calling init on the super probably undoes your efforts to set it beforehand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top