IOS控件-UIScrollView简介(二)

下文主要讲述如何生成一个UIScrollView,和通过按钮来控制UIScrollView的滚动效果


//  ViewController.h
//  Created by shenye on 16/4/21.
//  Copyright © 2016年 maomao365.com. All rights reserved.
// 
#import < UIKit / UIKit.h>
@interface ViewController : UIViewController
{ 
    UIScrollView * _uiScrollViewTest; //定义一个UIScrollView对象缓存当前对象
} 
@end

 
//  ViewController.m
//  Created by shenye on 16/4/21.
//  Copyright © 2016年 maomao365.com. All rights reserved.
 
#import "ViewController.h"
 
@interface ViewController ()

@end

@implementation ViewController
 //定义按钮事件,让按钮来控制UIScrollView的滚动效果
-(void)buttonTestClick:(UIButton *)sender{
    NSString *t =  [sender titleForState:UIControlStateNormal];
   CGPoint offset = _uiScrollViewTest.contentOffset; //获取当前UIScrollView对象的内容偏移量
    //根据不同的按钮,显示不同的滚动方式
   if([t  isEqual: @"垂直滚动"])
    {
    offset.y +=30;
    if(offset.y+_uiScrollViewTest.frame.size.height >_uiScrollViewTest.contentSize.height)
    {
        offset.y =_uiScrollViewTest.contentSize.height-_uiScrollViewTest.frame.size.height;
    }
    if(offset.y<0){
        offset.y =0;
    }   
    }
    
    
    if([t  isEqual: @"水平滚动"])
    {
        offset.x +=30;
        if(offset.x+_uiScrollViewTest.frame.size.width >_uiScrollViewTest.contentSize.width)
        {
            offset.x =_uiScrollViewTest.contentSize.width-_uiScrollViewTest.frame.size.width;
        }
        if(offset.x<0){
            offset.x =0;
        }
        
    }
    
    
    
    _uiScrollViewTest.contentOffset = offset;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //生成一个UIScrollView控件
    UIScrollView *uiScrollViewTest = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,600 , 600)];
    [uiScrollViewTest setBackgroundColor:[UIColor yellowColor]]; 
    [self.view addSubview:uiScrollViewTest]; //添加uiScrollView到当前视图中
    
    
    //创建一个图片view
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image =  [UIImage imageNamed:@"2.png"];
    CGFloat imageW = imageView.image.size.width;
    CGFloat imageH = imageView.image.size.height;
    imageView.frame = CGRectMake(0, 0, imageW, imageH);
    [uiScrollViewTest addSubview:imageView];
    
    //设置scrollView的滚动范围
    uiScrollViewTest.contentSize = imageView.image.size;
    
    //隐藏scrollView的垂直和水平的滚动条显示
    uiScrollViewTest.showsHorizontalScrollIndicator = NO;
    uiScrollViewTest.showsVerticalScrollIndicator   = NO;
    
    uiScrollViewTest.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);  //设置内容区域外的滚动边框
    

    _uiScrollViewTest = uiScrollViewTest;
    
    //view生成按钮控制滚动
    UIButton *buttonTest = [[UIButton alloc]initWithFrame:CGRectMake(330, 630, 100, 40)];
    
    [buttonTest setBackgroundColor:[UIColor grayColor]];
    [buttonTest addTarget:self action:@selector(buttonTestClick:)  forControlEvents:UIControlEventTouchDown];
    [buttonTest setTitle:@"垂直滚动" forState:UIControlStateNormal];
    [self.view addSubview:buttonTest];
    
    
    UIButton *buttonTest2 = [[UIButton alloc]initWithFrame:CGRectMake(230, 630, 100, 40)];
    
    [buttonTest2 setBackgroundColor:[UIColor grayColor]];
    [buttonTest2 addTarget:self action:@selector(buttonTestClick:)  forControlEvents:UIControlEventTouchDown];
    [buttonTest2 setTitle:@"水平滚动" forState:UIControlStateNormal];
    [self.view addSubview:buttonTest2];
  }

相关阅读:
IOS控件-UIScrollView简介(一)

IOS控件-UIScrollView简介(二)》上有2条评论

  1. Pingback引用通告: IOS控件-UIScrollView简介(一) | 猫猫小屋

  2. Pingback引用通告: IOS控件-UIScrollView简介(三)–缩放图片 | 猫猫小屋

评论已关闭。