准备工作不赘述
MainViewController.m
#import "MainViewController.h"
#import "LTView.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()<UIScrollViewDelegate>
@property(nonatomic,retain)UIScrollView *scrollView;
@end
@implementation MainViewController
-(void)dealloc
{
[_scrollView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
self.scrollView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:self.scrollView];
[self.scrollView release];
// 设置让scrollView滚动
self.scrollView.contentSize=CGSizeMake(WIDTH * 4, HEIGHT);
self.scrollView.pagingEnabled=YES;
self.scrollView.bounces=NO;
for (NSInteger i=1; i<5; i++) {
NSString *picName=[NSString stringWithFormat:@"%ld.jpg",i];
UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:picName]];
imageView.frame=CGRectMake(WIDTH * (i-1), 0, WIDTH, HEIGHT - 100);
// 把imageView放到scrollView中
[self.scrollView addSubview:imageView];
[imageView release];
}
// scrollView的缩放
// 缩放的比例
// 最大
self.scrollView.maximumZoomScale=2;
// 最小
self.scrollView.minimumZoomScale=0.5;
// 原始的缩放比例
self.scrollView.zoomScale=1;
self.scrollView.delegate=self;
UIPageControl *pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(90, 600, 200, 40)];
pageControl.backgroundColor=[UIColor redColor];
[self.view addSubview:pageControl];
[pageControl release];
// 图片个数和点的个数相同
pageControl.numberOfPages=4;
// 点的背景颜色
pageControl.pageIndicatorTintColor=[UIColor blueColor];
// 被选中的点的颜色
pageControl.currentPageIndicatorTintColor=[UIColor blackColor];
// 给它添加一个点击方法
[pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
pageControl.tag=1;
LTView *view=[[LTView alloc]initWithFrame:CGRectMake(0, 100, 375, 30)];
[self.view addSubview:view];
view.label.text=@"用户名";
view.textField.placeholder=@"请输入用户名";
view.textField.keyboardType=UIReturnKeyDefault;
[view release];
}
#pragma mark 专门用来缩放的协议方法
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews firstObject];
}
-(void)pageAction:(UIPageControl *)pageControl
{
NSLog(@"%ld",pageControl.currentPage);
// 触发事件,进行图片的切换
[self.scrollView setContentOffset:CGPointMake(pageControl.currentPage * WIDTH, 0)animated:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 滑动图片,让圆点跟着一起动
UIPageControl *pageControl=(UIPageControl *)[self.view viewWithTag:1];
pageControl.currentPage=self.scrollView.contentOffset.x/WIDTH;
}