ios開發(fā)日常 - 點(diǎn)擊頭像放大圖片

示例圖:

點(diǎn)擊放大圖.gif
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIImageView *lastImageView;
@property (nonatomic, assign)CGRect originalFrame;


-(void)showZoomImageView
{
    //scrollView作為背景
    UIScrollView *bgView = [[UIScrollView alloc] init];
    bgView.frame = [UIScreen mainScreen].bounds;
    bgView.backgroundColor = [UIColor blackColor];
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
    [bgView addGestureRecognizer:tapBg];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    NSURL *imgUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@/property/%@",BASEURL,[signModel headimgUrl]]];
    [imageView sd_setImageWithURL:imgUrl placeholderImage:[UIImage imageNamed:@"taskOwner"]];

//    imageView.frame = [bgView convertRect:picView.frame fromView:self.view];
    imageView.frame = CGRectMake(kScreen_Width - 80, 130, 0, 0);  //設(shè)置原始位置
    [bgView addSubview:imageView];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
    
    self.lastImageView = imageView;
    self.originalFrame = imageView.frame;
    self.scrollView = bgView;
    //最大放大比例
    self.scrollView.maximumZoomScale = 1.5;
    self.scrollView.delegate = self;
    
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = imageView.frame;
        frame.size.width = bgView.frame.size.width;
        frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
        frame.origin.x = 0;
        frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;
        imageView.frame = frame;
    }];
}

-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
    self.scrollView.contentOffset = CGPointZero;
    [UIView animateWithDuration:0.5 animations:^{
        self.lastImageView.frame = self.originalFrame;
        tapBgRecognizer.view.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [tapBgRecognizer.view removeFromSuperview];
        self.scrollView = nil;
        self.lastImageView = nil;
    }];
}

#pragma ScrollView //返回可縮放的視圖
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.lastImageView;
}

參考腳步之家網(wǎng)站的hebedich
http://www.jb51.net/article/74846.htm

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在此特此聲明:一下所有鏈接均來自互聯(lián)網(wǎng),在此記錄下我的查閱學(xué)習(xí)歷程,感謝各位原創(chuàng)作者的無私奉獻(xiàn) ! 技術(shù)一點(diǎn)一點(diǎn)積...
    遠(yuǎn)航的移動(dòng)開發(fā)歷程閱讀 11,545評(píng)論 12 197
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,113評(píng)論 25 709
  • iOS開發(fā)推薦DMG資源 http://blog.csdn.net/xiongbaoxr/article/deta...
    酸po萄閱讀 451評(píng)論 0 1
  • 我出生在一個(gè)普通的公務(wù)員家庭,我的童年在我的記憶里,幸福的時(shí)光莫過于夏天在院子里面和小朋友們捉螢火蟲,爸媽在旁邊圍...
    錦焱閱讀 354評(píng)論 0 5
  • 作為TVB的鐵粉,很久沒有關(guān)注港劇了。 當(dāng)年在親身去之前,我對(duì)香港的全部認(rèn)知來自TVB,鴨寮街的電器、深水埗的小店...
    034c9bcb0779閱讀 334評(píng)論 2 3

友情鏈接更多精彩內(nèi)容