滚动视图

MainViewController.h

#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController<UIScrollViewDelegate>
{
    UIScrollView * _scroll;
             
    UIPageControl * _page;
             
    NSMutableArray * _name;
             
    UIScrollView * _scroll1;
}
@end

MainViewController.m

#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)dealloc
{
        
    [super dealloc];
        
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
        
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
        
    return self;
}
- (void)viewDidLoad
{
        
    [super viewDidLoad];
    // Do any additional setup after loading the view.
        
    _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, 320, 200)];
    [_scroll setBackgroundColor:[UIColor blueColor]];
    [_scroll setContentSize:CGSizeMake(320*13, 200)];//宽或高,设置大小比屏幕小就没有滚动功能了;320*13:一张图片宽320,乘以13张图片宽度,几张图片就乘以几
    _scroll.contentOffset = CGPointMake(320, 0);//左上角偏移量
        
        
    _scroll.delegate = self;
    [_scroll setPagingEnabled:YES];//BOOL值 YES 设置图片不能停在中间
//    [_scroll setMinimumZoomScale:0.5f];//最小缩放倍数  Alt+Shift放到图片上松开Shift操作
//    [_scroll setMaximumZoomScale:2.0f];//最大缩放倍数  Alt+Shift放到图片上松开Shift操作
        
        
     [_scroll setBounces:NO];//默认是YES;如果是NO内容边界不反弹;如果是YES则反弹
//    [_scroll setAlwaysBounceVertical:YES];//默认是NO;如果是的,反弹是肯定的,即使内容小于边界,允许拖动垂直
//    [_scroll setAlwaysBounceHorizontal:NO];//默认是YES;如果是的,反弹是肯定的,即使内容小于边界,允许拖动水平
        
    //宽滚动条消失
      [_scroll setShowsHorizontalScrollIndicator:NO];//默认是YES
    //高滚动条消失
      [_scroll setShowsVerticalScrollIndicator:NO];//默认YES
        
      [self.view addSubview:_scroll];
      [_scroll release];
        
    for (int i = 0; i < 13; i++) {
            
       //Scroll1
        _scroll1 = [[UIScrollView alloc]initWithFrame:CGRectMake(320 * i, 0, 320, 200)];
        
        [_scroll1 setMinimumZoomScale:0.5f];//最小缩放倍数
        [_scroll1 setMaximumZoomScale:2.0f];//最大缩放倍数
        _scroll1.delegate = self;
        [_scroll addSubview:_scroll1];
        [_scroll1 release];
            
        //图片加到Scroll1
        _name = [[NSMutableArray alloc ]initWithObjects:@"JD",@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"JD",@"0", nil];
        UIImageView * iImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[_name objectAtIndex:i]]];
        iImage.frame = CGRectMake(0, 0, 320, 200);//相对于父视图坐标0,0;X:(320*i)+320
        [_scroll1 addSubview:iImage];
        [iImage release];
            
    }
      
    _page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 270, 320, 40)];
    [_page setBackgroundColor:[UIColor blueColor]];
    [_page setNumberOfPages:[_name count]-2];//设置点
    [_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    [_page setPageIndicatorTintColor:[UIColor redColor]];//改变不动的点的颜色
    [_page setCurrentPageIndicatorTintColor:[UIColor greenColor]];//改变动得点的颜色
    [self.view addSubview:_page];
    [_page release];
        
}
#pragma mark -
#pragma mark pageAction图片按钮
- (void)pageAction:(id)sender
{
        
    //UIPageControl * page = (UIPageControl *)sender;//转换类型id转UIPageControl
        
   // NSLog(@"%d",page.currentPage);
        
    _scroll.contentOffset = CGPointMake((_page.currentPage * 320)+320,0);//滚动偏移量;关联小点移动
        
}
#pragma mark -
#pragma mark ScrollView 滚动视图
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
        
  //  NSLog(@"%@",scrollView);
    NSLog(@"content off set == %@",NSStringFromCGPoint(scrollView.contentOffset));
        
    //滑动图片让小点跟着动
    [_page setCurrentPage:(scrollView.contentOffset.x - 320) / 320];
}
//方法:滑动块停止滑动,鼠标松开时停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
         //自动缩小
    for (UIScrollView * s1 in scrollView.subviews) {
        if ([s1 isKindOfClass:[UIScrollView class]]) {//判断是否是某个类
            [s1 setZoomScale:1.f];
        }
    }
        
        //循环
    if (_scroll.contentOffset.x == 320*12) {
        [_scroll setContentOffset:CGPointMake(320, 0)];//偏移量
            
        _page.currentPage = 0;
            
    }else if (_scroll.contentOffset.x == 0  ){
        [_scroll setContentOffset:CGPointMake(320 * 11, 0)];
        _page.currentPage = 11;
    }
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    NSArray * arr = [scrollView subviews];
    return [arr objectAtIndex:0];//获得要放大的那张视图
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end


本文出自 “Ghost霍的��博客” 博客,谢绝转载!

你可能感兴趣的:(import,interface,scroll)