• UIScrollView 实现比例缩放


     1 #import "RootViewController.h"
     2 
     3 @interface RootViewController ()<UIScrollViewDelegate>
     4 {
     5     UIImageView *imageView;
     6     UILabel *scaleRatioLabel;// 显示倍率用的Label
     7 }
     8 @property (nonatomic, strong)UIScrollView *scrollView;
     9 
    10 @end
    11 
    12 @implementation RootViewController
    13 
    14 - (void)dealloc
    15 {
    16     self.scrollView = nil;
    17 }
    18 
    19 - (void)viewDidLoad {
    20     [super viewDidLoad];
    21     self.scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    22     self.scrollView.delegate = self;
    23     [self.view addSubview:self.scrollView];
    24     imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1-3.jpg"]];
    25     [imageView setCenter:CGPointMake([UIScreen mainScreen].bounds.size.width/2.0, [UIScreen mainScreen].bounds.size.height/2.0)];
    26     [self.scrollView addSubview:imageView];
    27     //内容大小与图片大小一致
    28     self.scrollView.contentSize = imageView.frame.size;
    29     // 最小缩放比例
    30     self.scrollView.minimumZoomScale = 0.2f;
    31     // 最大缩放比例
    32     self.scrollView.maximumZoomScale = 5.0f;
    33     
    34     // 用来显示倍率的Label
    35     scaleRatioLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2.0 - 25, [UIScreen mainScreen].bounds.size.height/2.0 - 12.5, 50, 25)];
    36     [scaleRatioLabel setBackgroundColor:[UIColor clearColor]];
    37     [self.view addSubview:scaleRatioLabel];
    38     
    39 }
    40 
    41 #pragma mark - UIScrollViewDelegate
    42 // 设置要缩放的控件
    43 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
    44 {
    45     return imageView;
    46 }
    47 
    48 // 处理结束缩放事件
    49 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
    50 {
    51     [self.view bringSubviewToFront:scaleRatioLabel];
    52     [scaleRatioLabel setAlpha:0.6f];
    53     [scaleRatioLabel setBackgroundColor:[UIColor lightGrayColor]];
    54     scaleRatioLabel.text = [NSString stringWithFormat:@" x%.1f",scale];
    55     
    56     [UIView transitionWithView:scaleRatioLabel duration:2.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
    57         scaleRatioLabel.alpha = 0.0f;
    58     } completion:nil];
    59 }
    60 
    61 
    62 @end
  • 相关阅读:
    【C++】C++中的流
    【C++】C++中类的基本使用
    【Python】解析Python中的条件语句和循环语句
    【C++】C++中的分离式编译
    【C++】C++中assert和ENDEGU预处理语句
    【C++】C++中的函数的基本使用
    【算法】解析IEEE 754 标准
    【ThinkPHP】解析ThinkPHP5创建模块
    【C++】C++中的迭代器
    【算法】解析位运算
  • 原文地址:https://www.cnblogs.com/lantu1989/p/4646637.html
Copyright © 2020-2023  润新知