UIScrollView -------- 实现简单的相册功能

转自出处:UIScrollView -------- 实现简单的相册功能


@在MRC环境下:释放宏    #define RELEASE_SAFELY(__Pointer) do{[__Pointer release],__Pointer = nil;} while(0)

@思路:1个大的UIScrollView上嵌套N个小的UIScrollView ,小的UIScrollView实现缩放,大的UIScrollView实现滚动查看

1.定义一个HMTMyScrollView,继承自UIScrollView,用它来绘制放置图片UIImageView的小UIScrollView

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface HMTMyScrollView : UIScrollView <UIScrollViewDelegate>  
  4.   
  5. @property (nonatomic,retain)UIImageView * imageView;  
  6.   
  7. @end  
  8.   
  9. #import "HMTMyScrollView.h"  
  10.   
  11. @implementation HMTMyScrollView  
  12.   
  13.   
  14. -(void)dealloc{  
  15.   
  16.     RELEASE_SAFELY(_imageView);  
  17.     [super dealloc];  
  18.   
  19. }  
  20.   
  21. - (id)initWithFrame:(CGRect)frame  
  22. {  
  23.     self = [super initWithFrame:frame];  
  24.     if (self) {  
  25.         // Initialization code  
  26.           
  27.         // 缩放最大的比例  
  28.         self.maximumZoomScale = 3.0;  
  29.         // 缩放最小的比例  
  30.         self.minimumZoomScale = 0.2;  
  31.           
  32.         _imageView = [[UIImageView alloc]initWithFrame:[self bounds]];  
  33.         [self addSubview:_imageView];  
  34.           
  35.         self.delegate = self;  
  36.           
  37.     }  
  38.     return self;  
  39. }  
  40.   
  41. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{  
  42.   
  43.     return _imageView;  
  44. }  
  45. @end  

2.具体实现:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import "HMTRootViewController.h"  
  2. #import "HMTMyScrollView.h"  
  3.   
  4. #define HEIGHT   548  
  5. #define WIDTH    320  
  6. #define TAG      100  
  7.   
  8. @interface HMTRootViewController (){  
  9.   
  10.     NSInteger _previousPage;  
  11. }  
  12.   
  13. @property (nonatomic,retain)UIPageControl * pageControl;  
  14. @property (nonatomic,retain)UIScrollView  * headScroll;  
  15.   
  16. @end  
  17.   
  18. @implementation HMTRootViewController  
  19.   
  20. - (void)dealloc{  
  21.   
  22.     RELEASE_SAFELY(_pageControl);  
  23.     RELEASE_SAFELY(_headScroll);  
  24.     [super dealloc];  
  25.   
  26. }  
  27.   
  28. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  29. {  
  30.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  31.     if (self) {  
  32.         // Custom initialization  
  33.         _previousPage = 0;  
  34.           
  35.     }  
  36.     return self;  
  37. }  
  38.   
  39. - (void)viewDidLoad  
  40. {  
  41.     [super viewDidLoad];  
  42.     // Do any additional setup after loading the view.  
  43.     [self realizePhotoAlbun];  
  44. }  
  45.   
  46. - (void)realizePhotoAlbun{  
  47.   
  48.     self.headScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(00, WIDTH, HEIGHT)];  
  49.     _headScroll.backgroundColor = [UIColor grayColor];  
  50.     _headScroll.contentSize = CGSizeMake(WIDTH * 8, HEIGHT);  
  51.     _headScroll.bounces = NO;  
  52.     _headScroll.delegate = self;  
  53.     _headScroll.pagingEnabled = YES;  
  54.     _headScroll.showsHorizontalScrollIndicator = NO;  
  55.     [self.view addSubview:_headScroll];  
  56.     [_headScroll release];  
  57.       
  58.     NSArray * arrayImage = @[@"h1.jpeg",@"h2.jpeg",@"h3.jpeg",@"h4.jpeg",@"h5.jpeg",@"h6.jpeg",@"h7.jpeg",@"h8.jpeg"];  
  59.     for (int i = 0; i < [arrayImage count]; i++) {  
  60.           
  61.         HMTMyScrollView * photoScrollView = [[HMTMyScrollView alloc]initWithFrame:CGRectMake(WIDTH * i,0, WIDTH, HEIGHT)];  
  62.         photoScrollView.imageView.image = [UIImage imageNamed:[arrayImage objectAtIndex:i]];  
  63.         photoScrollView.tag = TAG + i;  
  64.         [_headScroll addSubview:photoScrollView];  
  65.         [photoScrollView release];  
  66.     }  
  67.       
  68.     self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(10051812040)];  
  69.     _pageControl.numberOfPages = 8;  
  70.     _pageControl.currentPageIndicatorTintColor = [UIColor purpleColor];  
  71.     _pageControl.pageIndicatorTintColor = [UIColor blackColor];  
  72.     [_pageControl addTarget:self action:@selector(controlPageControl:) forControlEvents:UIControlEventValueChanged];  
  73.     [self.view addSubview:_pageControl];  
  74.     [_pageControl release];  
  75.       
  76. }  
  77.   
  78. - (void)controlPageControl:(UIPageControl *)pageControl{  
  79.   
  80.     // 点击UIPageControl,翻页产生动画效果  
  81.     [self.headScroll setContentOffset:CGPointMake(pageControl.currentPage * WIDTH, 0) animated:YES];  
  82. }  
  83.   
  84. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{  
  85.   
  86.     NSInteger nowPage = scrollView.contentOffset.x / WIDTH;  
  87.     self.pageControl.currentPage = nowPage;  
  88.     HMTMyScrollView * myScrollView = (HMTMyScrollView *)[self.headScroll viewWithTag:_previousPage + TAG];  
  89.       
  90.     if (myScrollView.zoomScale != 1 && nowPage != _previousPage) {  
  91.         myScrollView.zoomScale = 1;  
  92.     }  
  93.       
  94.     _previousPage = nowPage;  
  95.     NSLog(@"%s",__FUNCTION__);  
  96.       
  97. }  
  98.   
  99. - (void)didReceiveMemoryWarning  
  100. {  
  101.     [super didReceiveMemoryWarning];  
  102.     // Dispose of any resources that can be recreated.  
  103. }  
  104.   
  105.   
  106.   
  107.   
  108. @end  


@自动循环滚动效果

[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];


//设置滚动
-(void)scrollTimer{   
    timerCount++;
    if (timerCount >= 4) {
        timerCount=0;
    }
    [adView setContentOffset:CGPointMake(tableWidth * timerCount, 0)animated:YES];
    [pageControl setCurrentPage:timerCount];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值