【IOS】圖片瀏覽效果(雙擊圖片,彈出層)

來源:互聯網
上載者:User
#import <UIKit/UIKit.h>#import <QuartzCore/QuartzCore.h>@interface EnlargeImageDoubleTap : UIImageView{    UIView *parentview;         //父視窗,即用將UIImageEx所加到的UIView    UIImageView *imageBackground;  //放大圖片後的背景    UIView* imageBackView;         //單獨查看時的背景        UIView* maskView;              //遮罩層    CGRect frameRect;}@property (nonatomic,retain) UIView *parentview;@property (nonatomic,retain) UIImageView *imageBackground;@property (nonatomic,retain) UIView* imageBackView;@property (nonatomic,retain) UIView* maskView;- (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer;//必須設定的- (void)setDoubleTap:(UIView*)imageView;@end

#import "EnlargeImageDoubleTap.h"@implementation EnlargeImageDoubleTap@synthesize parentview;@synthesize imageBackground,imageBackView,maskView;@interface EnlargeImageDoubleTap (private)- (void)fadeIn;- (void)fadeOut;- (void)closeImage:(id)sender;@end/* * setDoubleTap 初始化圖片 * @parent UIView 父視窗 */- (void)setDoubleTap:(UIView*) parent{    parentview=parent;    parentview.userInteractionEnabled=YES;    self.userInteractionEnabled=YES;        UITapGestureRecognizer *doubleTapRecognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];    doubleTapRecognize.numberOfTapsRequired = 2;    [doubleTapRecognize setEnabled :YES];    [doubleTapRecognize delaysTouchesBegan];    [doubleTapRecognize cancelsTouchesInView];        [self addGestureRecognizer:doubleTapRecognize];    }#pragma UIGestureRecognizer Handles/* * handleDoubleTap 雙擊圖片彈出單獨瀏覽圖片層 * recognizer 雙擊手勢 */-(void) handleDoubleTap:(UITapGestureRecognizer *)recognizer{                   if (imageBackView==nil) {                    if( [[UIDevice currentDevice] orientation]==UIDeviceOrientationLandscapeLeft||[[UIDevice currentDevice] orientation]==UIDeviceOrientationLandscapeRight)                    {                                                frameRect = CGRectMake(0, 0, parentview.frame.size.height+20, parentview.frame.size.width);                    }else                    {                           frameRect = CGRectMake(0, 0, parentview.frame.size.width, parentview.frame.size.height+20);                    }                                       imageBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.image.size.width+20, self.image.size.height+60)];                    imageBackView.backgroundColor = [UIColor grayColor];                    imageBackView.layer.cornerRadius = 10.0; //根據需要調整                                        [[imageBackView layer] setShadowOffset:CGSizeMake(10, 10)];                    [[imageBackView layer] setShadowRadius:5];                    [[imageBackView layer] setShadowOpacity:0.7];                    [[imageBackView layer] setShadowColor:[UIColor blackColor].CGColor];                                        maskView = [[UIView alloc]initWithFrame:frameRect];                    maskView.backgroundColor = [UIColor grayColor];                    maskView.alpha=0.7;                                        UIImage *imagepic = self.image;                    UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, self.image.size.width, self.image.size.height)];                    [view setImage:imagepic];                    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];                                        UIImage *closeimg = [UIImage imageNamed:@"closeImage.png"];                    btn.frame = CGRectMake(self.image.size.width-30,0, closeimg.size.width,closeimg.size.height);                    [btn setBackgroundImage:closeimg forState:UIControlStateNormal];                    [btn addTarget:self action:@selector(closeImage:) forControlEvents:UIControlEventTouchUpInside];                                        [imageBackView addSubview:view];                    [parentview addSubview:maskView];                    imageBackView.center= CGPointMake((frameRect.origin.x+frameRect.size.width)/2                                                      ,(frameRect.origin.y+frameRect.size.height)/2);                    [parentview addSubview:imageBackView];                    [imageBackView addSubview:btn];                    [parentview bringSubviewToFront:imageBackView];                    [self fadeIn];                                                       }}/* * fadeIn 圖片漸入動畫 */-(void)fadeIn{    imageBackView.transform = CGAffineTransformMakeScale(1.3, 1.3);    imageBackView.alpha = 0;    [UIView animateWithDuration:.55 animations:^{        imageBackView.alpha = 1;        imageBackView.transform = CGAffineTransformMakeScale(1, 1);    }];    }/* * fadeOut 圖片逐漸消失動畫 */- (void)fadeOut{    [UIView animateWithDuration:.35 animations:^{        imageBackView.transform = CGAffineTransformMakeScale(1.3, 1.3);        imageBackView.alpha = 0.0;    } completion:^(BOOL finished) {        if (finished) {            [imageBackView removeFromSuperview];        }    }];}/* * closeImage 關閉彈出圖片層 */-(void)closeImage:(id)sender{    [self fadeOut];    imageBackView=nil;    [maskView removeFromSuperview];    maskView=nil;}@end

運行後的:

例子的下載串連:http://download.csdn.net/detail/toss156/4160496

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.