zoukankan      html  css  js  c++  java
  • UIScrollView实现缩放和居中

    UIScrollView可以实现缩放功能。

    1. .h文件实现<UIScrollViewDelegate>协议。
    2. 设置scrollView对象的两个属性maximumZoomScale和minimumZoomScale。
    3. .m文件中实现代理方法viewForZoomingInScrollView:

    完成上面三个步骤就基本实现了缩放功能。

    .h文件

    1 @interface MapViewController : UIViewController <UIScrollViewDelegate>
    2 
    3 @end

    .m文件

     1 @implementation MapViewController
     2 
     3 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
     4 {
     5     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     6     if (self) {
     7         // Custom initialization
     8     }
     9     return self;
    10 }
    11 
    12 - (void)viewDidLoad
    13 {
    14     [super viewDidLoad];
    15     
    16     CGSize mainViewSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - 20 - 44 - 49);
    17     
    18     //scrollView
    19     UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, mainViewSize.width, mainViewSize.height)];
    20     scrollView.delegate = self;
    21     //开启多点触摸
    22     scrollView.multipleTouchEnabled = YES;
    23     //设置最小缩放比例
    24     scrollView.minimumZoomScale = 1.0;
    25     //设置最大缩放比例
    26     scrollView.maximumZoomScale = 10.0;
    27     [self.view addSubview:scrollView];
    28     
    29     
    30     UIImage *mapImage = [UIImage imageNamed:@"mapLarge.png"];
    31 
    32     
    33     CGFloat imageView_X = (mapImage.size.width > mainViewSize.width ? mainViewSize.width : mapImage.size.width);
    34     CGFloat imageView_Y = (mapImage.size.height > mainViewSize.height ? mainViewSize.height : mapImage.size.height);
    35     if (mapImage.size.width > mainViewSize.width) {
    36         imageView_Y = mapImage.size.height * (mainViewSize.width / mapImage.size.width);
    37     }
    38     UIImageView *mapImageView = [[UIImageView alloc] initWithFrame:CGRectMake((mainViewSize.width - imageView_X) / 2, (mainViewSize.height - imageView_Y) / 2, imageView_X, imageView_Y)];
    39     mapImageView.tag = 101;
    40     [mapImageView setImage:mapImage];
    41     [scrollView addSubview:mapImageView];
    42     
    43     
    44     
    45 }
    46 
    47 - (void)didReceiveMemoryWarning
    48 {
    49     [super didReceiveMemoryWarning];
    50     // Dispose of any resources that can be recreated.
    51 }
    52 
    53 #pragma mark -
    54 #pragma mark UIScrollView Delegate
    55 //实现图片在缩放过程中居中
    56 - (void)scrollViewDidZoom:(UIScrollView *)scrollView
    57 {
    58     CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
    59     CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
    60     [self.view viewWithTag:101].center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
    61 }
    62 
    63 //返回缩放的是哪个ImageView
    64 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    65     return [self.view viewWithTag:101];
    66 }
    67 
    68 @end
  • 相关阅读:
    hdoj 2063 过山车
    hdoj 2112 HDU Today
    hdoj 1874 畅通工程续
    hdoj 2544 最短路
    sound of the genuine
    复习webpack的常用loader
    node-sass安装报错
    react-debug
    react-router 4v 路由嵌套问题
    React 中使用sass
  • 原文地址:https://www.cnblogs.com/ubersexual/p/2980469.html
Copyright © 2011-2022 走看看