您的位置:首页 > 其它

滚动视图 解决弹出键盘遮住输入框的问题 实例解析

2015-12-13 12:09 477 查看
本节课以初始化scroll这个对象作为主体
 UIScrollView *scroll = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,
414, 736)];
    scroll.backgroundColor = [UIColorcyanColor];
    [self.viewaddSubview:scroll];
 
大纲:
/*
 
 * UIScrollView的概念
 
 * UIScrollView的重要属性
 
 *
解决登录注册界面的一个常见问题
 
 * UIScrollView常用属性
 
 *
利用scrollView放大图片
 */
 
一. UIScrollView的概念:
      是一个能够滚动的视图控件 能够滑动的视图都是继承与UIScrollView
  UIScrollView继承与UIView  
UIScrollView通过滚动来显示更多的内容
 
二.UIScrollView的重要属性
 
 1.contentSize就是UIScrollView的实际内容大小 注意与Frame的区别 
frame就是一个框   实际内容必须比它大
 
 2.contentOffset 偏移量 能够直接看到想看的内容 有多个视图通过偏移量可以把它放在自己想要的地方比如说一打开某个界面就看到的视图通过滑动可以看到其他的视图
 
 3.contentInset 需要赋值一个UIEdgeInsetsMake类型的方法后有4个值
 给scroll增加额外的滚动区域(注意参数顺序 上 左 下 右)
 scroll.contentInset = UIEdgeInsetsMake(50, 50, 50, 50);
 注意: contentSize就是UIScrollView的实际内容大小 如果contentSize的尺寸比滚动视图对象的frame小或者相等 则不能滑动(注意)
 
一个实例解决键盘覆盖输入框的问题[/b]
例如:模拟器运行在4s上的例题
 UIScrollView *scrollow = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,
320, 480)];
    
   //给UIScorllView设置内容大小 这样的话不会滚因为contentSize与滚动视图对象的frame一样大
   scrollow.contentSize =CGSizeMake(320,480);
    
   
//给UIScrollView增加滚动区域,254是键盘的高度
   scrollow.contentInset =UIEdgeInsetsMake(0,0,254,
0);
    scrollow.backgroundColor = [UIColorredColor];
//把滚动视图给控制器自带的视图 也可以用addSubview也可以用赋值号
   
self.view =scrollow;
    
   
UITextField *textFiled = [[UITextField
alloc]initWithFrame:CGRectMake(0,420,
100, 60)];
    
   textFiled.backgroundColor = [UIColorcolorWithRed:0.262green:0.907blue:0.110
alpha:1.000];
    
   [scrollow
addSubview:textFiled];
    
   
UITextField *textFiled1 = [[UITextFieldalloc]initWithFrame:CGRectMake(0,100,
100, 60)];
    
   textFiled1.backgroundColor = [UIColorgrayColor];
    
   [scrollow
addSubview:textFiled1];
    
 UITextField *textFiled2 = [[UITextFieldalloc]initWithFrame:CGRectMake(0,420,
100, 60)];
   textFiled2.backgroundColor = [UIColorblackColor];
   [scrollowaddSubview:textFiled2];
 
 
三.UIScrollView的常用属性
       1.是否隐藏水平滚动条
   scroll.showsHorizontalScrollIndicator =NO;
   2.是否隐藏垂直滚动条
   scroll.showsVerticalScrollIndicator= NO;
   3.设置是否有回滚效果
   scroll.bounces = NO;
  4.设置是否能够滚动
  scroll.scrollEnabled = NO;
  5.设置指示器(即滚动条)的样式
    scroll.indicatorStyle =UIScrollViewIndicatorStyleWhite;
   scroll.contentSize =CGSizeMake(414*3,736*3);
  6.滚动视图可以翻页
    scroll.pagingEnabled  =YES;
 
.利用scroll放大视图(用到代理)
 1.新建图片视图 并赋值一张图片注意放在滚动视图上
 
 scroll.contentSize =CGSizeMake(img.size.width, img.size.height);
//   设置最大放大倍率
    scroll.maximumZoomScale =2.0;
//   设置最小放大倍率
    scroll.minimumZoomScale =1.0;
//   挂上代理
    scroll.delegate =self;
    
//   减速速率
    scroll.decelerationRate =0.5;
    
 
 
//在代理方法里指定要放大的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
   
return imageView;
    
    
}
 

实例解析滚动视图
1.定义全局变量

{

    UILabel *showLabel;

}

  初始化滚动视图对象

  UIScrollView *showView = [[UIScrollViewalloc]initWithFrame:[UIScreenmainScreen].bounds];

    设置滚动条的样式

    showView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

    隐藏滚动条

    showView.showsVerticalScrollIndicator =NO;

    设置是否允许翻页

    showView.pagingEnabled =
YES;

   重点 挂上代理  因为后边会用到代理方法

    showView.delegate =
self;

    把滚动视图放在self.view上

    [self.viewaddSubview:showView];

    工程中图片的名字

    NSArray *images =
@[@"狗",@"猫",@"鸡",@"鸭",@"鹿"];

    

   
//    设置滚动视图内容的尺寸

    showView.contentSize =
CGSizeMake(CGRectGetWidth([UIScreenmainScreen].bounds)*images.count,0);

     

    for (int i=0; i<images.count; i++) {

       初始化图片视图  并设置frame

        UIImageView *imageView = [[UIImageViewalloc]
initWithFrame:CGRectMake(CGRectGetWidth([UIScreenmainScreen].bounds)*i,0,
CGRectGetWidth([UIScreenmainScreen].bounds),CGRectGetHeight([UIScreenmainScreen].bounds))];

        imageView.image = [UIImageimageNamed:images[i]];

        imageView.contentMode =UIViewContentModeScaleAspectFit;

        图片视图默认不与用户交互   需要打开权限

        imageView.userInteractionEnabled =
YES;

        [showView addSubview:imageView];

        

       在滚动视图上方  显示  图片的标题以及内容

        UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(200,50,
200, 50)];

        label.text = images[i];

        label.textColor = [UIColororangeColor];

        label.font = [UIFontsystemFontOfSize:50];

        [imageView addSubview:label];

        

//        如果是最后一张图片

在最后一张图片上加上其他按钮并给响应事件

        if (images.count-1 == i) {

            

            UIButton *button =  [UIButtonbuttonWithType:UIButtonTypeCustom];

            button.frame =CGRectMake(CGRectGetWidth([UIScreenmainScreen].bounds)-150,CGRectGetHeight([UIScreenmainScreen].bounds)-80,100,
40);

            [button setTitle:@"进入>"forState:UIControlStateNormal];

            [button setTitle:@"欢迎✌"forState:UIControlStateHighlighted];

            button.backgroundColor = [UIColororangeColor];

            [button addTarget:selfaction:@selector(toHome)forControlEvents:UIControlEventTouchUpInside];

            

            [imageView addSubview:button];

            

        }

        

    }

      这个标签是拖拽的时候显示的

    showLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,CGRectGetHeight([UIScreenmainScreen].bounds)-40,CGRectGetWidth([UIScreenmainScreen].bounds),40)];

    showLabel.text =@"欢迎使用动物世界";

    showLabel.textColor = [UIColororangeColor];

    showLabel.alpha =0.7;

    showLabel.hidden =YES;

    [showView addSubview:showLabel];

    

}

#pragma mark---滚动视图代理方法

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    

    showLabel.hidden =NO;

}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{

    showLabel.hidden =YES;

}

- (void)toHome{

    AppDelegate *app = [UIApplicationsharedApplication].delegate;

    [app showHome];

}

 
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: