zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    (转载) mysql中,option是保留字段么?
    (转载)腾讯CMEM的PHP扩展
    (转载)一句简单命令重启nginx
    (转载)四种常见的 POST 提交数据方式
    (转载)完美解决PHP中文乱码问题
    (转载)file_get_contents("php://input")
    (转载)PHP 下 CURL 通过 POST 提交表单失败的原因之一与解决办法
    (转载)php array_merge 和 两数组相加区别
    Immutable-不变模式与不变类
    zookeeper 编程框架 curator
  • 原文地址:https://www.cnblogs.com/lantu1989/p/4646637.html
Copyright © 2011-2022 走看看