這是封裝的一個(gè)UITapGestureRecognizer呀袱,這里記錄一下
主要思路
點(diǎn)擊圖片的時(shí)候洛口,首先在window的keywindow上面再創(chuàng)建一個(gè)view慌烧,然后把圖片等比例放大炫乓,放到上面冀墨,然后操作捏合闸衫,移動(dòng)
UIWindow * window = [UIApplication sharedApplication].keyWindow;
backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, windowWidth, windowHeight)];
backView.backgroundColor = [UIColor blackColor];
backView.alpha = 0;
[window addSubview:backView];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideBackView)];
[backView addGestureRecognizer:tap];
UIImageView * backImageView = (UIImageView *)self.view;
oldRect = backImageView.frame;
UIImageView * showImageView = [[UIImageView alloc]initWithFrame:[self.view convertRect:self.view.bounds toView:window]];
showImageView.image = backImageView.image;
showImageView.tag = 190231;
showImageView.userInteractionEnabled = YES;
[backView addSubview:showImageView];
//縮放手勢(shì)
UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[showImageView addGestureRecognizer:pinch];
//移動(dòng)手勢(shì)
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[showImageView addGestureRecognizer:pan];
//放大圖片的主要代碼
[UIView animateWithDuration:0.3 animations:^{
showImageView.frame = CGRectMake(0,(windowHeight - backImageView.image.size.height * windowWidth / backImageView.image.size.width) / 2, windowWidth, backImageView.image.size.height * windowWidth / backImageView.image.size.width);
backView.alpha = 1;
newRect = showImageView.frame;
}];
下面是手勢(shì)完成的主要操作
縮放手勢(shì)這里關(guān)鍵是找到放大的比例,首先要理清楚要放大的倍數(shù)是多少,如果直接使用sender.scale 這樣就是在前一次放大的基礎(chǔ)上诽嘉,再次放大那么多倍蔚出,而我們實(shí)際要的效果是在前一次放大的基礎(chǔ)上弟翘,再放大移動(dòng)的數(shù)值,所以每次手指移動(dòng)放大的倍數(shù)應(yīng)該是上次的sender.scale值減去當(dāng)前的sender.scale
UIImageView * showImageView = (UIImageView *)[backView viewWithTag:190231];
if (sender.state == UIGestureRecognizerStateBegan) {
pinchChangeValue = 1;
return;
}
CGFloat change = 1 - (pinchChangeValue - sender.scale);
showImageView.transform = CGAffineTransformScale(showImageView.transform, change, change);
//記錄當(dāng)前比例
pinchChangeValue = sender.scale;
移動(dòng)手勢(shì)
//這里和縮放差不多相同的原理
if (sender.state == UIGestureRecognizerStateBegan) {
panChangeValueOld = sender.view.center;
return;
}
if (sender.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [sender translationInView:self.view];
float newY = panChangeValueOld.y + translation.y;
float newX = panChangeValueOld.x + translation.x;
sender.view.center = CGPointMake(newX, newY);
}
最后是點(diǎn)擊收起圖片:
這里要處理兩次的隱藏骄酗,點(diǎn)擊的時(shí)候判斷圖片是否被放大了稀余,如果被縮放手勢(shì)放大的話,先變?yōu)辄c(diǎn)擊放大后的尺寸趋翻,再變?yōu)樵瘸叽缇α眨绻麤]有被縮放手勢(shì)放大,則直接變?yōu)樵瓉沓叽?/p>
UIImageView * showImageView = (UIImageView *)[backView viewWithTag:190231];
if (showImageView.frame.size.width > newRect.size.width) {
[UIView animateWithDuration:0.3 animations:^{
showImageView.frame = newRect;
}];
}else{
[UIView animateWithDuration:0.3 animations:^{
showImageView.frame = oldRect;
backView.alpha = 0;
} completion:^(BOOL finished) {
[backView removeFromSuperview];
}];
}
本次練習(xí)的demo的地址:demo