UIScrollView的简单例子

来源:互联网 发布:爱心公益活动网站知乎 编辑:程序博客网 时间:2024/05/29 10:25

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  

 

  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