`
jsntghf
  • 浏览: 2478419 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

UIScrollView的简单例子

    博客分类:
  • iOS
阅读更多

MyScrollView.h

 

#import <UIKit/UIKit.h>

@interface MyScrollView : UIScrollView <UIScrollViewDelegate> {
	UIImage *image;
	UIImageView *imageView;
}

@property (nonatomic, retain) UIImage *image;

@end

 

MyScrollView.m

 

#import "MyScrollView.h"

@implementation MyScrollView

@synthesize image;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
		self.delegate = self;
		self.minimumZoomScale = 0.5;
		self.maximumZoomScale = 2.5;
		self.showsVerticalScrollIndicator = NO;
		self.showsHorizontalScrollIndicator = NO;
		
		imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
		imageView.contentMode = UIViewContentModeCenter;
		[self addSubview:imageView];
    }
    return self;
}

- (void)setImage:(UIImage *)img {
	imageView.image = img;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {	
	return imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    CGFloat zs = scrollView.zoomScale;
	zs = MAX(zs, 1.0);
	zs = MIN(zs, 2.0);	
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.3];		
	scrollView.zoomScale = zs;	
	[UIView commitAnimations];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
	
	if ([touch tapCount] == 2) {
		CGFloat zs = self.zoomScale;
		zs = (zs == 1.0) ? 2.0 : 1.0;
		
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.3];			
		self.zoomScale = zs;	
		[UIView commitAnimations];
	}
}

- (void)dealloc {
	[image release];
	[imageView release];
	
    [super dealloc];
}

@end

 

MainViewController.h

 

#import "MyScrollView.h"

@interface MainViewController : UIViewController <UIScrollViewDelegate> {
	IBOutlet UIScrollView *scrView;
	
	NSInteger lastPage;
}

@end

 

MainViewController.m

 

#import "MainViewController.h"

@implementation MainViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	
	self.view.backgroundColor = [UIColor blackColor];
	
	scrView.contentSize = CGSizeMake(1700, 480);
	scrView.showsHorizontalScrollIndicator = NO;
	
	for (int i = 0; i < 5; i++) {
		MyScrollView *ascrView = [[MyScrollView alloc] initWithFrame:CGRectMake(340 * i, 0, 320, 480)];
		NSString *imgPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d", i + 1] ofType:@"jpg"];
		ascrView.image = [UIImage imageWithContentsOfFile:imgPath];
		ascrView.tag = 100 + i;
		
		[scrView addSubview:ascrView];
		[ascrView release];
	}
	
	lastPage = 0;
}

//划动的动画结束后调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
	CGFloat pageWidth = scrollView.frame.size.width;
	NSInteger page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
	if (lastPage != page) {
		MyScrollView *aView = (MyScrollView *)[scrView viewWithTag:100 + lastPage];
		aView.zoomScale = 1.0;
		
		lastPage = page;
	}
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	scrView = nil;
}

- (void)dealloc {
	[scrView release];
    [super dealloc];
}

@end
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics