因?yàn)轫?xiàng)目需要容达,需要對(duì)圖片進(jìn)行一些操作。本著拿來(lái)主義的原則~上網(wǎng)找了方法垂券。但是發(fā)現(xiàn)網(wǎng)上很多實(shí)現(xiàn)董饰,要么過(guò)于復(fù)雜,要么不夠完善。終于找到一篇比較好的文章卒暂。
參考文章: http://apluck.iteye.com/blog/1781607
// 添加所有的手勢(shì)
- (void) addGestureRecognizerToView:(UIView *)view
{
// 旋轉(zhuǎn)手勢(shì)
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
[view addGestureRecognizer:rotationGestureRecognizer];
// 縮放手勢(shì)
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
[view addGestureRecognizer:pinchGestureRecognizer];
// 移動(dòng)手勢(shì)
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
[view addGestureRecognizer:panGestureRecognizer];
}
// 處理旋轉(zhuǎn)手勢(shì)
- (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer? {
UIView *view = rotationGestureRecognizer.view;
if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged)
{
view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);
[rotationGestureRecognizer setRotation:0];
}
}
// 處理縮放手勢(shì)
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
UIView *view = pinchGestureRecognizer.view;
if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged)
{
view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);? ? ? ? ? pinchGestureRecognizer.scale = 1;
}
}
// 處理拖拉手勢(shì)
- (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer
{
UIView *view = panGestureRecognizer.view;
if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [panGestureRecognizer translationInView:view.superview];
[view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
[panGestureRecognizer setTranslation:CGPointZero inView:view.superview];? ? ? }
}
這樣只需要簡(jiǎn)單調(diào)用
[self addGestureRecognizerToView:view];
//如果處理的是圖片啄栓,別忘了
[imageView setUserInteractionEnabled:YES];
[imageView setMultipleTouchEnabled:YES];
大功告成。
具體使用一下:在.h文件里邊定義變量
@interface YourViewController : UIViewController
{
CGFloat lastScale;
CGRect oldFrame;? ? //保存圖片原來(lái)的大小
CGRect largeFrame;? //確定圖片放大最大的程度
}
然后在viewDidLoad里面加上
- (void)viewDidLoad
{
[super viewDidLoad];
showImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
[showImgView setMultipleTouchEnabled:YES];
[showImgView setUserInteractionEnabled:YES];
[showImgView setImage:[UIImage imageNamed:@"1.jpg"]];
oldFrame = showImgView.frame;
largeFrame = CGRectMake(0 - screenSize.width, 0 - screenSize.height, 3 * oldFrame.size.width, 3 * oldFrame.size.height);
[self addGestureRecognizerToView:showImgView];
[self.view addSubview:showImgView];
}
這樣就實(shí)現(xiàn)了但是也祠,這樣是不夠的昙楚。因?yàn)槔镞叺目s放和移動(dòng)等沒(méi)有做相應(yīng)的判斷。因?yàn)榇a很簡(jiǎn)潔诈嘿,所以擴(kuò)展也非常方便堪旧。我修改了縮放的代碼,增加了限制奖亚,其他的類似
// 處理縮放手勢(shì)
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
UIView *view = pinchGestureRecognizer.view;
if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged)
{
view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
if (showImgView.frame.size.width < oldFrame.size.width)
{
showImgView.frame = oldFrame;
//讓圖片無(wú)法縮得比原圖小
}
if (showImgView.frame.size.width > 3 * oldFrame.size.width)
{
showImgView.frame = largeFrame;
}
pinchGestureRecognizer.scale = 1;
}
}
這樣就好了淳梦。保證了圖片的最大和最小比例。