iOS使用UIScrollerView进行图片缩放,实现仿新闻图片阅读功能

来源:互联网 发布:淘宝买管制刀具 编辑:程序博客网 时间:2024/05/01 18:02

这是一个简单的仿一些新闻客户端图片查看功能,但是这个完成的比较简单只能点击查看一张照片,使用UIScrollerView实现了一个图片缩放的功能。

#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong) UIImageView *imageView;@property (nonatomic, strong) UIScrollView *scrollView;@property (nonatomic, strong) UIButton *delBtn;@property (nonatomic, strong) UILabel *textLabel;@property (nonatomic, strong) UITapGestureRecognizer *recognizer;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.        //初始化UIScrollerView占满整个屏幕    self.scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    [self.view addSubview:self.scrollView];        //初始化一张图片,居中显示    UIImage *image = [UIImage imageNamed:@"222.jpg"];    self.imageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    self.imageView.image = image;    self.imageView.contentMode = UIViewContentModeCenter;    [self.scrollView addSubview:self.imageView];    //设置代理,并设置最大和最小缩放比例    self.scrollView.delegate = self;    self.scrollView.maximumZoomScale = 2;    self.scrollView.minimumZoomScale = 1.0;    //初始化一个按钮    self.delBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];    [self.delBtn setFrame:CGRectMake(10, 20, 30, 30)];    [self.view addSubview:self.delBtn];    [self.delBtn setBackgroundColor:[UIColor clearColor]];    //初始化一个label    self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 620, 350, 20)];    self.textLabel.text = @"This is just a test, Can be more complex";    self.textLabel.backgroundColor = [UIColor clearColor];    self.textLabel.textColor = [UIColor whiteColor];    [self.view addSubview:self.textLabel];        //初始化手势操作,并为UIView添加手势控制    self.recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];    [self.view addGestureRecognizer:self.recognizer];    self.recognizer.numberOfTapsRequired = 1;    self.recognizer.numberOfTouchesRequired = 1;        [self.view setBackgroundColor:[UIColor blackColor]];}//代理需要实现的函数,返回要被缩放的UIView控件,即为UIImageView- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {    return self.imageView;}//点击屏幕后的操作- (void)tapView:(UITapGestureRecognizer*)gesture {    if (self.delBtn.hidden)        [self showSubView];    else        [self hideSubView];}//以动画的方式使按钮消失- (void)hideSubView {    CATransition *animation = [CATransition animation];    animation.type = kCATransitionFade;    animation.duration = 0.3;    [self.delBtn.layer addAnimation:animation forKey:nil];    [self.textLabel.layer addAnimation:animation forKey:nil];    self.delBtn.hidden = YES;    self.textLabel.hidden = YES;}//以动画的方式显示按钮- (void)showSubView {    CATransition *animation = [CATransition animation];    animation.type = kCATransitionReveal;    animation.duration = 0.3;    [self.delBtn.layer addAnimation:animation forKey:nil];    [self.textLabel.layer addAnimation:animation forKey:nil];    self.delBtn.hidden = NO;    self.textLabel.hidden = NO;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end



此时不能进行滑动,这符合我们得要求,点击屏幕后会发现左上角的按钮和下方的标签都渐近消失了。




单击并进行缩放之后的效果,可以上下左右移动查看未显示的图片,在此处并没有很好的实现,垂直滑动会有很大一片空白。


0 0
原创粉丝点击