• 【转】几个经常需要自定义的View总结


    分类: Iphone应用开发 2011-12-28 15:13 136人阅读 评论(0) 收藏 举报

    为了独立出组件的一些功能,如,为UIbutton切换背景图片,我们经常需要自定义一些组件,下面是我经常用到的,先总结出来,以后会慢慢更新:

    -:UIScroview

    srollview的事件经常与其子view事件冲突,截断子view事件的相应

    //传递touch事件

    - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event

    {

       if(!self.dragging)

         

      {

            [[selfnextResponder]touchesBegan:toucheswithEvent:event];

      }

       

      [supertouchesBegan:touches withEvent:event];

       

      // NSLog(@"MyScrollView touch Began");

    }

     

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

    {

       if(!self.dragging)

      {

            [[selfnextResponder]touchesMoved:toucheswithEvent:event];

      }

      [supertouchesMoved:touches withEvent:event];

    }

     

     

     

    - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event

    {

       if(!self.dragging)

      {

            [[selfnextResponder]touchesEnded:toucheswithEvent:event];

      }

      [supertouchesEnded:touches withEvent:event];

    }

     
    1.   


     

    //父视图是否可以将消息传递给子视图,yes是将事件传递给子视图,则不滚动,no是不传递则继续滚动

    - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view

    {

      if ([view isKindOfClass:[CustomUITextViewclass]])

      {

         return YES;

      }

      else 

      {

        returnNO;

       

      }

     

    }

     

    //Yes是子视图取消继续接受touch消息(可以滚动),NO是子视图可以继续接受touch事件(不滚动)

    //默认的情况下当view不是一个UIControlo类的时候,值是yes,否则是no 

    //调用情况是这样的一般是在发送tracking messages消息后会调用这个函数,来判断scroll是否滚动,还是接受子视图的touch事件

    - (BOOL)touchesShouldCancelInContentView:(UIView *)view

    {

      NSLog(@"用户点击的视图 %@",view);

       returnNO;


    二:UITextView默认是没有边框的,可以给它加个凹下去的边框

    -(void) drawRect:(CGRect)rect {

       

        [self.layersetBackgroundColor: [[UIColorwhiteColor]CGColor]];

        [self.layersetBorderColor: [[UIColorgrayColor]CGColor]];

        [self.layersetBorderWidth:1.0];

        [self.layersetCornerRadius:8.0f];

        [self.layersetMasksToBounds:YES];

       UIGraphicsBeginImageContext(self.frame.size);

       CGContextRef currentContext =UIGraphicsGetCurrentContext();

      CGContextSetLineWidth(currentContext, 2.0);

      CGContextSetRGBStrokeColor(currentContext, 0.6,0.6,.6, 1.0);

      CGRect myRect = CGContextGetClipBoundingBox(currentContext);  

      float myShadowColorValues[] = {0,0,0,1};

       CGColorSpaceRef myColorSpace =CGColorSpaceCreateDeviceRGB();

      CGColorRef colorRef = CGColorCreate(myColorSpace, myShadowColorValues);

      CGContextSetShadowWithColor(currentContext, CGSizeMake(-1,1),2, colorRef);

       

      CGContextStrokeRect(currentContext, myRect);

       UIImage *backgroundImage = (UIImage *)UIGraphicsGetImageFromCurrentImageContext();

       UIImageView *myImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];

      [myImageView setImage:backgroundImage];

      [selfaddSubview:myImageView];

      [myImageView release];

       UIGraphicsEndImageContext();

    }


    三:我们会想按下按钮时,切换button的图片背景,可以给UIbutton加个UIControllEvent事件的消息通知,当按钮被按下的时候,通知按钮所有者去切换图片

    - (id)initWithFrame:(CGRect)_frame  {

    if (self = [superinitWithFrame:_frame]) {

    [selfaddTarget:selfaction:@selector(touchDown:)forControlEvents:UIControlEventTouchDown];

    [selfaddTarget:selfaction:@selector(touchUpInside:)forControlEvents:UIControlEventTouchUpInside];

    //[selfaddTarget:selfaction:@selector(touchUpOutside:)forControlEvents:UIControlEventTouchUpOutside];

    }

     

    returnself;

    }

     

    - (void)touchDown:(id)sender {

    NSNotification *notification = [NSNotificationnotificationWithName:@"TouchDownButton"object:selfuserInfo:nil];

    [[NSNotificationCenterdefaultCenter]postNotification:notification];

    NSLog(@"%s",__FUNCTION__);

    }

     

    - (void)touchUpInside:(id)sender {

    //[self setBackgroundImage:@"next.png" forState:UIControlStateNormal];

    NSNotification *notification = [NSNotificationnotificationWithName:@"TouchUpButton"object:selfuserInfo:nil];

    [[NSNotificationCenterdefaultCenter]postNotification:notification];

     

    }

    使用方法
    在所有者类中定义这些自定义的组件,如定义

    CustomerButton *nextButton;

    监听消息

    [notification addObserver:self selector:@selector(touchDownNext) name:@"TouchDownButton" object:nil];

    [notification addObserver:self selector:@selector(touchUpNext) name:@"TouchUpButton" object:nil];

    监听到后需要执行的动作

    -(void)touchDownNext{

    UIImage *image = [UIImageimageNamed:@"next_pressed.png"];

    [nextButtonsetBackgroundImage:imageforState:UIControlStateHighlighted];

    }

     

    -(void)touchUpNext{

    UIImage *image = [UIImageimageNamed:@"next.png"];

    [nextButtonsetBackgroundImage:imageforState:UIControlStateNormal];

    }

    不能上传附件,有需要的可以联系我
  • 相关阅读:
    AngularJS ui-router (嵌套路由)
    解决Can't connect to MySQL server on 'localhost' (10048)
    PHP获取一年有几周以及每周开始日期和结束日期
    MySQL(八)之DML
    MySQL(七)MySQL常用函数
    MySQL(六)之MySQL常用操作符
    MySQL(五)之DDL(数据定义语言)与六大约束
    MySQL(四)之MySQL数据类型
    MySQL(三)之SQL语句分类、基本操作、三大范式
    linux命令详解之netstat
  • 原文地址:https://www.cnblogs.com/wengzilin/p/2418136.html
Copyright © 2020-2023  润新知