zoukankan      html  css  js  c++  java
  • iphone UIScrollView缩放

    allImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
    allImageScrollView.minimumZoomScale = 0.3;
    allImageScrollView.maximumZoomScale = 1.0;
    allImageScrollView.backgroundColor = [UIColor clearColor];
    allImageScrollView.delegate = self;
    [self.view addSubview:allImageScrollView];

    mPicStatusesViewController = [[PicStatusesViewController alloc] init];
    [allImageScrollView addSubview:mPicStatusesViewController.view];

    //UIScrollView Delegete 实现

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

    {
        return mPicStatusesViewController.view; //返回ScrollView上添加的需要缩放的视图
    }

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView

    {
        //缩放操作中被调用
    }

    - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale

    {
        //缩放结束后被调用
      }

    原文地址:http://ios-iphone.diandian.com/post/2011-09-15/5066984

     
     
     

    MyScrollView.h

     

    Ios代码  收藏代码
    1. #import <UIKit/UIKit.h>  
    2.   
    3. @interface MyScrollView : UIScrollView <UIScrollViewDelegate> {  
    4.     UIImage *image;  
    5.     UIImageView *imageView;  
    6. }  
    7.   
    8. @property (nonatomic, retain) UIImage *image;  
    9.   
    10. @end  

     

    MyScrollView.m

     

    Ios代码  收藏代码
    1. #import "MyScrollView.h"  
    2.   
    3. @implementation MyScrollView  
    4.   
    5. @synthesize image;  
    6.   
    7. - (id)initWithFrame:(CGRect)frame {  
    8.     if ((self = [super initWithFrame:frame])) {  
    9.         self.delegate = self;  
    10.         self.minimumZoomScale = 0.5;  
    11.         self.maximumZoomScale = 2.5;  
    12.         self.showsVerticalScrollIndicator = NO;  
    13.         self.showsHorizontalScrollIndicator = NO;  
    14.           
    15.         imageView = [[UIImageView alloc] initWithFrame:CGRectMake(00, self.frame.size.width, self.frame.size.height)];  
    16.         imageView.contentMode = UIViewContentModeCenter;  
    17.         [self addSubview:imageView];  
    18.     }  
    19.     return self;  
    20. }  
    21.   
    22. - (void)setImage:(UIImage *)img {  
    23.     imageView.image = img;  
    24. }  
    25.   
    26. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {   
    27.     return imageView;  
    28. }  
    29.   
    30. - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {  
    31.     CGFloat zs = scrollView.zoomScale;  
    32.     zs = MAX(zs, 1.0);  
    33.     zs = MIN(zs, 2.0);    
    34.       
    35.     [UIView beginAnimations:nil context:NULL];  
    36.     [UIView setAnimationDuration:0.3];        
    37.     scrollView.zoomScale = zs;    
    38.     [UIView commitAnimations];  
    39. }  
    40.   
    41. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
    42.     UITouch *touch = [touches anyObject];  
    43.       
    44.     if ([touch tapCount] == 2) {  
    45.         CGFloat zs = self.zoomScale;  
    46.         zs = (zs == 1.0) ? 2.0 : 1.0;  
    47.           
    48.         [UIView beginAnimations:nil context:NULL];  
    49.         [UIView setAnimationDuration:0.3];            
    50.         self.zoomScale = zs;      
    51.         [UIView commitAnimations];  
    52.     }  
    53. }  
    54.   
    55. - (void)dealloc {  
    56.     [image release];  
    57.     [imageView release];  
    58.       
    59.     [super dealloc];  
    60. }  
    61.   
    62. @end  

     

    MainViewController.h

     

    Ios代码  收藏代码
    1. #import "MyScrollView.h"  
    2.   
    3. @interface MainViewController : UIViewController <UIScrollViewDelegate> {  
    4.     IBOutlet UIScrollView *scrView;  
    5.       
    6.     NSInteger lastPage;  
    7. }  
    8.   
    9. @end  

     

    MainViewController.m

     

    Ios代码  收藏代码
    1. #import "MainViewController.h"  
    2.   
    3. @implementation MainViewController  
    4.   
    5. - (void)viewDidLoad {  
    6.     [super viewDidLoad];  
    7.       
    8.     self.view.backgroundColor = [UIColor blackColor];  
    9.       
    10.     scrView.contentSize = CGSizeMake(1700480);  
    11.     scrView.showsHorizontalScrollIndicator = NO;  
    12.       
    13.     for (int i = 0; i < 5; i++) {  
    14.         MyScrollView *ascrView = [[MyScrollView alloc] initWithFrame:CGRectMake(340 * i, 0320480)];  
    15.         NSString *imgPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d", i + 1] ofType:@"jpg"];  
    16.         ascrView.image = [UIImage imageWithContentsOfFile:imgPath];  
    17.         ascrView.tag = 100 + i;  
    18.           
    19.         [scrView addSubview:ascrView];  
    20.         [ascrView release];  
    21.     }  
    22.       
    23.     lastPage = 0;  
    24. }  
    25.   
    26. //划动的动画结束后调用  
    27. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {  
    28.     CGFloat pageWidth = scrollView.frame.size.width;  
    29.     NSInteger page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;  
    30.       
    31.     if (lastPage != page) {  
    32.         MyScrollView *aView = (MyScrollView *)[scrView viewWithTag:100 + lastPage];  
    33.         aView.zoomScale = 1.0;  
    34.           
    35.         lastPage = page;  
    36.     }  
    37. }  
    38.   
    39. - (void)didReceiveMemoryWarning {  
    40.     [super didReceiveMemoryWarning];  
    41. }  
    42.   
    43. - (void)viewDidUnload {  
    44.     scrView = nil;  
    45. }  
    46.   
    47. - (void)dealloc {  
    48.     [scrView release];  
    49.     [super dealloc];  
    50. }  
    51.   
    52. @end  
    本文转载:http://2015.iteye.com/blog/1575003 
     
  • 相关阅读:
    golang实现dns域名解析(一)
    互联网协议入门(一)(转)
    DNS入门(转)
    随笔:Golang 时间Time
    mysql查询某一个字段是否包含中文字符
    screen状态变Attached连接会话失败
    golang :连接数据库闲置断线的问题
    神奇的GO语言:空接口(interface)
    Go语言:变参函数
    go语言:函数参数传递详解
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3323608.html
Copyright © 2011-2022 走看看