zoukankan      html  css  js  c++  java
  • iOS 基础控件(下)

      上篇介绍了UIButton、UILabel、UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView。

    UIScrollView

      顾名思义也知道这个是和滚动相关的控件,在Android开发时遇到过ScrollView,当内容的尺寸超出了屏幕范围之后,用ScrollView则可以通过滚动的方式使得超出屏幕的那部分内容通过滚动的方式显示出来,在Android里面有水平的ScrollView和垂直的ScrollView,在iOS里面就只有一个ScrollView,而且这个ScrollView的功能更大,某些功能已经超出了ScrollView的作用范围了。下面则看一下ScrollView的一些属性

    • contentSize:CGSize类型,ScrollView的内容的实际大小;
    • contentOffset:CGPoint类型,ScrollView当前滚动到的位置,以视图的左上角来定位;
    • contentInset:UIEdgeInsets类型,用于增加ScrollView内容的滚动范围,相当于给ScrollView的四周补白;

    下面则是其他属性

    • bounces:BOOL类型,是否有弹簧效果
    • scrollEnabled   :BOOL类型,是否能滚动
    • showsHorizontalScrollIndicator:BOOL类型,是否显示水平方向的滚动条
    • showsVerticalScrollIndicator:BOOL类型,是否显示垂直方向的滚动条
    • indicatorStyle:UIScrollViewIndicatorStyle类型,设定滚动条的样式,这个枚举类型有三个值
       UIScrollViewIndicatorStyleDefault
       UIScrollViewIndicatorStyleBlack
       UIScrollViewIndicatorStyleWhite
    • dragging:BOOL类型,是否正在被拖拽
    • tracking:BOOL类型,当touch后还没有拖动的时候值是YES,否则NO
    • decelerating:BOOL类型,是否正在减速
    • zooming:BOOL类型, 是否正在缩放

    ScrollView的最直接的功能就是滑动,但是但是它还兼备着手势缩放(在Android中需要在ImageView中使用Matrix),还有滑动翻页的功能(在Android中是通过ViewPager是实现),下面都通过代码来实现,在viewDidLoad方法中加入

        UIImage *image=[UIImage imageNamed:@"Android Struct2.gif"];
        self.imageView=[[UIImageView alloc]initWithImage:image];
        self.imageView.tag=22;
        [self.scrollView addSubview:self.imageView];

      单纯通过上面的代码则可以实现滑动查看一幅图片的功能,一个UIScrollView其实也是一个容器,它里面的子空间都是通过addSubview方法添加进去,而最重要的则是通过设置contentSize这个属性来告诉UIScrollView究竟子控件的尺寸有多大。

      手势缩放的示例是在上面的基础上再作修改,如果只需要单纯地实现手势缩放,主要是实现了UIScrollView的- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView方法,同时也要设置UIScrollView的最大最小的缩放比例:minimumZoomScale属性和maximumZoomScale属性,在viewDidLoad方法中加入

        UIImage *image=[UIImage imageNamed:@"Android Struct2.gif"];
        self.imageView=[[UIImageView alloc]initWithImage:image];
        self.imageView.tag=22;
        [self.scrollView addSubview:self.imageView];
        
        self.scrollView.contentSize=imageView.image.size;
        self.scrollView.maximumZoomScale=5;
        self.scrollView.minimumZoomScale=0.2;
        self.scrollView.delegate=self;

    在视图控制器里面的类中加入下面的方法

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

    如果要额外加一些手势则需要使用UITapGestureRecognizer,在viewDidLoad中加入

        UITapGestureRecognizer *twoFingerTapRecongizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped)];
        twoFingerTapRecongizer.numberOfTapsRequired=1;
        twoFingerTapRecongizer.numberOfTouchesRequired=2;
        [self.scrollView addGestureRecognizer:twoFingerTapRecongizer];

    再定义下面这个方法

    -(void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer
    {
        CGFloat newZoomScale=self.scrollView.zoomScale/1.5f;
        newZoomScale =MAX(newZoomScale,self.scrollView.minimumZoomScale);
        
        [self.scrollView setZoomScale:newZoomScale animated:true];
    }

      接下来说的是翻页功能,要想翻页功能最好是结合另一个控件UIPageControl,关于这个控件有几个属性要列举一下

    • numberOfPages : 总页数
    • currentPage : 当前的页码
    • hidesForSinglePage : 当只有一页的时候,是否要隐藏视图监听

    要让UIScrollView进入翻页模式则需要设置pagingEnabled属性,把它设置成True。代码如下,在viewDidLoad中加入

     1     CGFloat w=self.view.frame.size.width;
     2     CGFloat h=self.view.frame.size.height;
     3     for(int i=0;i<3;i++)
     4     {
     5         UIImageView *imageView=[[UIImageView alloc]init];
     6         imageView.frame=CGRectMake(i*w, 0, w, h);
     7         imageView.image=[UIImage imageNamed:@"African Daisy.gif"];
     8         
     9         [self.scrollView addSubview:imageView];
    10     }
    11     
    12     self.scrollView.contentSize=CGSizeMake( 3*w, 0);
    13     self.scrollView.showsHorizontalScrollIndicator=false;
    14     self.scrollView.pagingEnabled=true;
    15     self.scrollView.delegate=self;
    16 
    17 UIPageControl *pagecontrol=[[UIPageControl alloc] init];
    18 pagecontrol.center=CGPointMake(w*0.5,h-20);
    19 pagecontrol.bounds=CGRectMake(0, 0, 150, 50);
    20 pagecontrol.numberOfPages=3;
    21 
    22 pagecontrol.pageIndicatorTintColor=[UIColor grayColor];
    23 pagecontrol.currentPageIndicatorTintColor=[UIColor whiteColor];
    24 
    25 pagecontrol.enabled=false;
    26 [self.view addSubview:pagecontrol];
    27 _pageControl=pagecontrol;
    28 
    29 -(void) scrollViewDidScroll:(UIScrollView *)scrollView
    30 {
    31     int page=scrollView.contentOffset.x/scrollView.frame.size.width;
    32     _pageControl.currentPage=page;
    33 }

    UIAlertView

      UIAlertView是消息弹窗,它的作用与效果不用多说了,要使用UIAlertView的就需要让控制器实现UIAlertViewDelegate协议。有用到的属性如下

    • title:消息框的标题
    • message:消息框的内容
    • numberOfButtons:按钮总数
    • cancelButtonTitle:取消按钮的标题
    • cancelButtonIndex:取消按钮的索引
    • firstOtherButtonIndex:第一个其他类型按钮的索引
    • visible:消息框可视
    • alertViewStyle:UIAlertViewStyle类型,是消息框的类型,它是一个枚举类型,它的值如下
    • UIAlertViewStyleDefault 只弹信息和按钮
    • UIAlertViewStyleSecureTextInput 有一个textfield加密框
    • UIAlertViewStylePlainTextInput 有一个不加密的textfield
    • UIAlertViewStyleLoginAndPasswordInput 有两个textfield,Login和password

    对于上面提到的按钮索引,是取消按钮和其他按钮组成的一个集合,其中第一个一般是Cancel按钮,它的索引是0,如果Cancel按钮没有设置,则它会是-1,其他则按添加的顺序。在iOS的消息框中,只有Cancel有特有名称,其他按钮则没有特有名称,都统称为OtherButton。

    消息框的其中一个构造函数如下

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest"  
                                                       message:@"message"  
                                                      delegate:self  
                                             cancelButtonTitle:@"Cancel"  
    otherButtonTitles:@"OtherBtn1",@"OtherBtn2",@"OtherBtn3",nil];

    上面otherButtonTitles参数可以添加多个按钮。添加按钮除了在构造函数里面弄之外,还可以通过方法添加。如果不需要Cancel按钮,则给参数传一个nil值

    [alert addButtonWithTitle:@"addButton"];

    让消息框显示则需要调用下面的方法

    [alert show];

    消息框还有一系列的事件

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        //这个事件在消息框上面的按钮被点击之后触发,通过buttonIndex来确定究竟是哪个按钮被点击,然后采取相应的动作
    }

    还有其他6个事件方法

     1 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
     2 {
     3     //AlertView已经消失时执行的事件
     4 }
     5 
     6 -(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
     7 {
     8     //ALertView即将消失时的事件
     9 }
    10 
    11 -(void)alertViewCancel:(UIAlertView *)alertView
    12 {
    13     //AlertView的取消按钮的事件,但这个事件一直我没有触发过
    14 }
    15 
    16 -(void)didPresentAlertView:(UIAlertView *)alertView
    17 {
    18     //AlertView已经显示时的事件
    19 }
    20 
    21 -(void)willPresentAlertView:(UIAlertView *)alertView
    22 {
    23     //AlertView即将显示时
    24 }
    25 
    26 -(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
    27 {
    28     //第一个触发的事件
    29 }

    对于上面一系列的事件大体可以分为两类,显示的事件,他们触发顺序如下

    alertViewShouldEnableFirstOtherButton——>willPresentAlertView——>didPresentAlertView

    另一类是消失的事件,是在点击后才会触发

    clickedButtonAtIndex——>(如果会触发视图取消,则会调用alertViewCancel)willDismissWithButtonIndex——>didDismissWithButtonIndex

  • 相关阅读:
    在bindingNavigator1中加入具有更好体验性的DateTimePicker
    static的初始化顺序 (转)
    C#数据结构求最大公约数和最小公倍数[辗转相除法]
    DataGridView控件显示行号
    C# 小票打印机 直接打印 无需驱动[转]
    Core Data 中遇到的一些问题
    字符指针不分配存储区,字符常量存储于静态数据区
    传送门
    Error Set
    实现类似iPhone通讯录新增名片,保存,之后可进行编辑操作的功能
  • 原文地址:https://www.cnblogs.com/HopeGi/p/4295927.html
Copyright © 2011-2022 走看看