IOS 微信 聊天雙擊放大圖片操作

單擊圖片放大,瀏覽圖片細節(jié)伐憾,之后再單擊回到原始頁面,
如果在瀏覽細節(jié)時候赫模,雙擊圖片 會局部放大树肃,也可以雙手拖拽放大,

newImage.gif

代碼參考

#import "TransImageTool.h"

#define kSCREEN_WIDTH    ([UIScreen mainScreen].bounds.size.width)
#define kSCREEN_HEIGHT   ([UIScreen mainScreen].bounds.size.height)
#define kMaxZoom 3

@interface TransImageTool ()<UIScrollViewDelegate>

@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) BOOL isTwiceTaping;
@property (nonatomic, assign) BOOL isDoubleTapingForZoom;
@property (nonatomic, assign) CGFloat currentScale;
@property (nonatomic, assign) CGFloat offsetY;
@property (nonatomic, assign) CGFloat touchX;
@property (nonatomic, assign) CGFloat touchY;

@property (nonatomic, strong) UIImageView *transImageView;

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIButton *userBtn ;
@property (nonatomic, strong) UIView *userBtnContainer ;




@end

static CGRect oldframe;

@implementation TransImageTool

- (void)showImage:(UIImageView *)avatarImageView{
    
    UIImage *image = avatarImageView.image;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    oldframe = [avatarImageView convertRect:avatarImageView.bounds toView:window];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:oldframe];
    imageView.image = image;
    
    self.transImageView = imageView;
 
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT)];
    
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor blackColor];
    scrollView.maximumZoomScale = 5.0;
    
    CGFloat ratio = _width / _height * kSCREEN_HEIGHT / kSCREEN_WIDTH;
    CGFloat min = MIN(ratio, 1.0);
    scrollView.minimumZoomScale = min;
    
    self.scrollView = scrollView;
    
    UITapGestureRecognizer *onetap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    
    [self.scrollView addGestureRecognizer:onetap];
    [self.scrollView addSubview:imageView];
    
    
    UITapGestureRecognizer *tapImgViewTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImgViewHandleTwice:)];
    tapImgViewTwice.numberOfTapsRequired = 2;
    tapImgViewTwice.numberOfTouchesRequired = 1;
    
    [scrollView addGestureRecognizer:tapImgViewTwice];
    //如果雙擊失敗就單擊
    [onetap requireGestureRecognizerToFail:tapImgViewTwice];
    
    [window addSubview:self.scrollView];
    
    CGFloat imageViewX = 0;
    CGFloat imageViewY = (kSCREEN_HEIGHT - image.size.height*kSCREEN_WIDTH/image.size.width) / 2;
    CGFloat imageViewW =  kSCREEN_WIDTH;
    CGFloat imageViewH = image.size.height * kSCREEN_WIDTH/image.size.width;
    
    [UIView animateWithDuration:0.3
                     animations:^
    {
        imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
                         
    }
    completion:^(BOOL finished)
    {
                         
    }];
    
    
}



- (void)hideImage:(UITapGestureRecognizer*)tap
{
    
    UIView *backgroundView = tap.view;

    self.userBtnContainer.alpha = 0;
    
    [UIView animateWithDuration:0.3
                     animations:^
    {
         self.transImageView.frame = oldframe;
         self.scrollView.alpha = 0;
                         
     }
     completion:^(BOOL finished)
    {
                         
         [backgroundView removeFromSuperview];
         [self.scrollView removeFromSuperview];
         [self.userBtnContainer removeFromSuperview];
    }];
    
}

#pragma mark - UIScrollViewDelegate -

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    self.currentScale = scale;
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    return self.transImageView;
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    
    CGFloat xcenter = scrollView.center.x;
    CGFloat ycenter = scrollView.center.y;

    xcenter = scrollView.contentSize.width > kSCREEN_WIDTH?scrollView.contentSize.width / 2 : xcenter;
    ycenter = scrollView.contentSize.height > kSCREEN_HEIGHT ?scrollView.contentSize.height / 2 : ycenter;
    
    if(_isDoubleTapingForZoom)//是否是雙擊放大的
    {
        if (_touchX>0 && _touchY>0)
        {
            //點擊在圖片上
            CGFloat transformX = _touchX * kMaxZoom;
            CGFloat transformY = _touchY * kMaxZoom;
            CGFloat contentW = scrollView.contentSize.width;
            CGFloat contentH = scrollView.contentSize.height;

            if (transformX + kSCREEN_WIDTH *.5>=contentW)
            {
                //右邊太大瀑罗。
                transformX = kSCREEN_WIDTH*(kMaxZoom-1);
            }
            else
            {
                if (transformX-kSCREEN_WIDTH *.5 < 0)
                {//左邊太小
                    transformX = 0;
                }
                else
                {
                    transformX = transformX-kSCREEN_WIDTH *0.5;
                }
            }
            //計算Y
            //Y的放大比例跟圖片的本身有關胸嘴,需要從新計算內容的高度和屏幕的尺寸關系

            int maxCount = contentH /([UIScreen mainScreen].bounds.size.height);
            
            if (maxCount >= 1)
            {
                //智能移動到中心
                if (transformY-contentH *0.5 < 0)
                {
                    //上邊太小雏掠,智能移動到邊界
                    transformY = 0;
                }
                else
                {
                    
                    if (transformY +kSCREEN_HEIGHT *.5 > contentH) {
                       //下邊太大,智能移動到邊界
                        transformY = contentH - kSCREEN_HEIGHT;
                    }
                    else
                    {
                        transformY = transformY - kSCREEN_HEIGHT *.5;

                    }
                    
                }
            }
            else
            {
                if (transformY-contentH *0.5 < 0)
                {
                    //上邊太小劣像,智能移動到邊界
                    transformY = 0;
                }
                else
                {
                    //下邊太大乡话,智能移動到邊界
                    transformY = ycenter - kSCREEN_HEIGHT*0.5;
                }
            }
            
            [scrollView setContentOffset:CGPointMake(transformX, transformY)];
        }
        else
        {
        //默認放大位置
          [scrollView setContentOffset:CGPointMake(xcenter- kSCREEN_WIDTH *.5, ycenter - kSCREEN_HEIGHT*0.5)];
        }
        
    }
   [self.transImageView setCenter:CGPointMake(xcenter, ycenter)];
    _touchY = 0;
    _touchX = 0;
}

-(void)tapImgViewHandleTwice:(UIGestureRecognizer *)sender{
    
    _touchX = [sender locationInView:self.transImageView].x;
    _touchY = [sender locationInView:self.transImageView].y;
    
    if (_touchY >CGRectGetHeight(self.transImageView.frame))
    {
        //如果大于最大的就是在圖片外面,默認放大
        _touchY = 0;
    }
 
    if(_isTwiceTaping)//雙擊
    {
        return;
    }
    _isTwiceTaping = YES;
    
    
    if(_currentScale > 1.0)
    {
        _currentScale = 1.0;
        [_scrollView setZoomScale:1.0 animated:YES];
    }
    else
    {
        _isDoubleTapingForZoom = YES;
        _currentScale = kMaxZoom;
        [_scrollView setZoomScale:kMaxZoom animated:YES];
    }
    _isDoubleTapingForZoom = NO;
    //延時做標記判斷驾讲,使用戶點擊3次時的單擊效果不生效蚊伞。
    [self performSelector:@selector(twiceTaping) withObject:nil afterDelay:0.65];
}

-(void)twiceTaping{

    _isTwiceTaping = NO;
}

//是否添加其他按鈕
- (void)addTooBtnView
{
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, kSCREEN_WIDTH, 80)];
    containerView.backgroundColor = [UIColor clearColor];
    self.userBtnContainer = containerView;
    // [window addSubview:containerView];
    /*添加button*/
    UIButton *userBtnright = [UIButton buttonWithType:UIButtonTypeSystem];
    userBtnright.frame =CGRectMake(kSCREEN_WIDTH - 100, 0, 60, 30);
    [userBtnright setTitle:@"正確" forState:UIControlStateNormal];
    [userBtnright setBackgroundColor:[UIColor blueColor]];
    self.userBtn = userBtnright;
    //[containerView addSubview:userBtnright];
    /*添加button*/
    UIButton *userBtnerror = [UIButton buttonWithType:UIButtonTypeSystem];
    userBtnerror.frame =CGRectMake(kSCREEN_WIDTH - 170, 0, 60, 30);
    [userBtnerror setTitle:@"錯誤" forState:UIControlStateNormal];
    [userBtnerror setBackgroundColor:[UIColor blueColor]];
    self.userBtn = userBtnerror;
    //[containerView addSubview:userBtnerror];
}

-(void)dealloc
{
    NSLog(@"dealloc");

}

@end

Demo地址
https://github.com/WhereFindYouAgin/PhotographEnlarge

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市吮铭,隨后出現的幾起案子时迫,更是在濱河造成了極大的恐慌,老刑警劉巖谓晌,帶你破解...
    沈念sama閱讀 211,743評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件掠拳,死亡現場離奇詭異,居然都是意外死亡纸肉,警方通過查閱死者的電腦和手機溺欧,發(fā)現死者居然都...
    沈念sama閱讀 90,296評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來柏肪,“玉大人姐刁,你說我怎么就攤上這事》澄叮” “怎么了聂使?”我有些...
    開封第一講書人閱讀 157,285評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長谬俄。 經常有香客問我柏靶,道長,這世上最難降的妖魔是什么溃论? 我笑而不...
    開封第一講書人閱讀 56,485評論 1 283
  • 正文 為了忘掉前任屎蜓,我火速辦了婚禮,結果婚禮上钥勋,老公的妹妹穿的比我還像新娘炬转。我一直安慰自己,他們只是感情好算灸,可當我...
    茶點故事閱讀 65,581評論 6 386
  • 文/花漫 我一把揭開白布返吻。 她就那樣靜靜地躺著,像睡著了一般乎婿。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上街佑,一...
    開封第一講書人閱讀 49,821評論 1 290
  • 那天谢翎,我揣著相機與錄音捍靠,去河邊找鬼。 笑死森逮,一個胖子當著我的面吹牛榨婆,可吹牛的內容都是我干的。 我是一名探鬼主播褒侧,決...
    沈念sama閱讀 38,960評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼良风,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了闷供?” 一聲冷哼從身側響起烟央,我...
    開封第一講書人閱讀 37,719評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎歪脏,沒想到半個月后疑俭,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 44,186評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡婿失,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,516評論 2 327
  • 正文 我和宋清朗相戀三年钞艇,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片豪硅。...
    茶點故事閱讀 38,650評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡哩照,死狀恐怖,靈堂內的尸體忽然破棺而出懒浮,到底是詐尸還是另有隱情飘弧,我是刑警寧澤,帶...
    沈念sama閱讀 34,329評論 4 330
  • 正文 年R本政府宣布嵌溢,位于F島的核電站眯牧,受9級特大地震影響,放射性物質發(fā)生泄漏赖草。R本人自食惡果不足惜学少,卻給世界環(huán)境...
    茶點故事閱讀 39,936評論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望秧骑。 院中可真熱鬧版确,春花似錦、人聲如沸乎折。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,757評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽骂澄。三九已至吓蘑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背磨镶。 一陣腳步聲響...
    開封第一講書人閱讀 31,991評論 1 266
  • 我被黑心中介騙來泰國打工溃蔫, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人琳猫。 一個月前我還...
    沈念sama閱讀 46,370評論 2 360
  • 正文 我出身青樓伟叛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親脐嫂。 傳聞我的和親對象是個殘疾皇子统刮,可洞房花燭夜當晚...
    茶點故事閱讀 43,527評論 2 349

推薦閱讀更多精彩內容