@在MRC环境下:释放宏 #define RELEASE_SAFELY(__Pointer) do{[__Pointer release],__Pointer = nil;} while(0)
@思路:1个大的UIScrollView上嵌套N个小的UIScrollView ,小的UIScrollView实现缩放,大的UIScrollView实现滚动查看
1.定义一个HMTMyScrollView,继承自UIScrollView,用它来绘制放置图片UIImageView的小UIScrollView
#import <UIKit/UIKit.h>
@interface HMTMyScrollView : UIScrollView <UIScrollViewDelegate>
@property (nonatomic,retain)UIImageView * imageView;
@end
#import "HMTMyScrollView.h"
@implementation HMTMyScrollView
-(void)dealloc{
RELEASE_SAFELY(_imageView);
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// 缩放最大的比例
self.maximumZoomScale = 3.0;
// 缩放最小的比例
self.minimumZoomScale = 0.2;
_imageView = [[UIImageView alloc]initWithFrame:[self bounds]];
[self addSubview:_imageView];
self.delegate = self;
}
return self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return _imageView;
}
@end
2.具体实现:
#import "HMTRootViewController.h"
#import "HMTMyScrollView.h"
#define HEIGHT 548
#define WIDTH 320
#define TAG 100
@interface HMTRootViewController (){
NSInteger _previousPage;
}
@property (nonatomic,retain)UIPageControl * pageControl;
@property (nonatomic,retain)UIScrollView * headScroll;
@end
@implementation HMTRootViewController
- (void)dealloc{
RELEASE_SAFELY(_pageControl);
RELEASE_SAFELY(_headScroll);
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_previousPage = 0;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self realizePhotoAlbun];
}
- (void)realizePhotoAlbun{
self.headScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
_headScroll.backgroundColor = [UIColor grayColor];
_headScroll.contentSize = CGSizeMake(WIDTH * 8, HEIGHT);
_headScroll.bounces = NO;
_headScroll.delegate = self;
_headScroll.pagingEnabled = YES;
_headScroll.showsHorizontalScrollIndicator = NO;
[self.view addSubview:_headScroll];
[_headScroll release];
NSArray * arrayImage = @[@"h1.jpeg",@"h2.jpeg",@"h3.jpeg",@"h4.jpeg",@"h5.jpeg",@"h6.jpeg",@"h7.jpeg",@"h8.jpeg"];
for (int i = 0; i < [arrayImage count]; i++) {
HMTMyScrollView * photoScrollView = [[HMTMyScrollView alloc]initWithFrame:CGRectMake(WIDTH * i,0, WIDTH, HEIGHT)];
photoScrollView.imageView.image = [UIImage imageNamed:[arrayImage objectAtIndex:i]];
photoScrollView.tag = TAG + i;
[_headScroll addSubview:photoScrollView];
[photoScrollView release];
}
self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(100, 518, 120, 40)];
_pageControl.numberOfPages = 8;
_pageControl.currentPageIndicatorTintColor = [UIColor purpleColor];
_pageControl.pageIndicatorTintColor = [UIColor blackColor];
[_pageControl addTarget:self action:@selector(controlPageControl:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_pageControl];
[_pageControl release];
}
- (void)controlPageControl:(UIPageControl *)pageControl{
// 点击UIPageControl,翻页产生动画效果
[self.headScroll setContentOffset:CGPointMake(pageControl.currentPage * WIDTH, 0) animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger nowPage = scrollView.contentOffset.x / WIDTH;
self.pageControl.currentPage = nowPage;
HMTMyScrollView * myScrollView = (HMTMyScrollView *)[self.headScroll viewWithTag:_previousPage + TAG];
if (myScrollView.zoomScale != 1 && nowPage != _previousPage) {
myScrollView.zoomScale = 1;
}
_previousPage = nowPage;
NSLog(@"%s",__FUNCTION__);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@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];
}