//
// PinchView.m
// ViewDemo
//
// Created by wgd on 12-4-25.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "PinchView.h"
#import <QuartzCore/QuartzCore.h>
@implementation PinchView
@synthesize imageView;
@synthesize image;
@synthesize backView;
@synthesize showView;
-(PinchView *)initWithFrame:(CGRect)backRect andDest:(CGRect)destRect
{
if (self = [super initWithFrame:backRect])
{
self.backView = [[UIView alloc] initWithFrame:backRect];
backView.backgroundColor = [UIColor grayColor];
[self addSubview:backView];
[backView release];
self.imageView = [[UIImageView alloc] init];
[self.backView addSubview:imageView];
[imageView release];
self.showView = [[UIView alloc] initWithFrame:destRect];
[self addSubview:showView];
[showView release];
UIPinchGestureRecognizer *pinGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleSize:)];
[showView addGestureRecognizer:pinGesture];
[pinGesture release];
}
return self;
}
-(void)setImage:(UIImage *)image_
{
if(image)
{
[image release];
image = nil;
}
image = image_;
[image_ retain];
CGPoint centerPoint;
centerPoint = self.showView.center;
//imageView.center = centerPoint;
//imageView.frame = CGRectMake(centerPoint.x-image.size.width/2,centerPoint.y-image.size.height/2,image.size.width, image.size.height);
imageView.frame = CGRectMake(showView.frame.origin.x - backView.frame.origin.x,
showView.frame.origin.y - backView.frame.origin.y,
showView.frame.size.width,
showView.frame.size.height);
imageView.image = image;
}
-(void)dealloc
{
[imageView release];
[image release];
[backView release];
[showView release];
[super dealloc];
}
#pragma mark -
#pragma mark scaleSize
-(void)scaleSize:(UIPinchGestureRecognizer *)pinGesture
{
// NSLog(@"come");
CGPoint locationInView = [pinGesture locationInView:imageView];
//imageView.layer.anchorPoint = CGPointMake(locationInView.x/imageView.frame.size.width,locationInView.y/imageView.frame.size.height);
//imageView.layer.anchorPoint = CGPointMake(0, 0);
NSLog(@"anchorPoint x:%f y:%",imageView.layer.anchorPoint.x,imageView.layer.anchorPoint.y);
if(imageView.frame.size.width <= 50 && [pinGesture scale] <1)
{
return;
}
if(imageView.frame.size.width >= imageView.image.size.width && [pinGesture scale] >1)
{
return;
}
imageView.transform = CGAffineTransformScale([imageView transform],
[pinGesture scale],
[pinGesture scale]);
}
@end
// PinchView.m
// ViewDemo
//
// Created by wgd on 12-4-25.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "PinchView.h"
#import <QuartzCore/QuartzCore.h>
@implementation PinchView
@synthesize imageView;
@synthesize image;
@synthesize backView;
@synthesize showView;
-(PinchView *)initWithFrame:(CGRect)backRect andDest:(CGRect)destRect
{
if (self = [super initWithFrame:backRect])
{
self.backView = [[UIView alloc] initWithFrame:backRect];
backView.backgroundColor = [UIColor grayColor];
[self addSubview:backView];
[backView release];
self.imageView = [[UIImageView alloc] init];
[self.backView addSubview:imageView];
[imageView release];
self.showView = [[UIView alloc] initWithFrame:destRect];
[self addSubview:showView];
[showView release];
UIPinchGestureRecognizer *pinGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleSize:)];
[showView addGestureRecognizer:pinGesture];
[pinGesture release];
}
return self;
}
-(void)setImage:(UIImage *)image_
{
if(image)
{
[image release];
image = nil;
}
image = image_;
[image_ retain];
CGPoint centerPoint;
centerPoint = self.showView.center;
//imageView.center = centerPoint;
//imageView.frame = CGRectMake(centerPoint.x-image.size.width/2,centerPoint.y-image.size.height/2,image.size.width, image.size.height);
imageView.frame = CGRectMake(showView.frame.origin.x - backView.frame.origin.x,
showView.frame.origin.y - backView.frame.origin.y,
showView.frame.size.width,
showView.frame.size.height);
imageView.image = image;
}
-(void)dealloc
{
[imageView release];
[image release];
[backView release];
[showView release];
[super dealloc];
}
#pragma mark -
#pragma mark scaleSize
-(void)scaleSize:(UIPinchGestureRecognizer *)pinGesture
{
// NSLog(@"come");
CGPoint locationInView = [pinGesture locationInView:imageView];
//imageView.layer.anchorPoint = CGPointMake(locationInView.x/imageView.frame.size.width,locationInView.y/imageView.frame.size.height);
//imageView.layer.anchorPoint = CGPointMake(0, 0);
NSLog(@"anchorPoint x:%f y:%",imageView.layer.anchorPoint.x,imageView.layer.anchorPoint.y);
if(imageView.frame.size.width <= 50 && [pinGesture scale] <1)
{
return;
}
if(imageView.frame.size.width >= imageView.image.size.width && [pinGesture scale] >1)
{
return;
}
imageView.transform = CGAffineTransformScale([imageView transform],
[pinGesture scale],
[pinGesture scale]);
}
@end