• 【转】几个经常需要自定义的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];

    }

    不能上传附件,有需要的可以联系我
  • 相关阅读:
    大道至简第二章读后感
    读大道至第一章简有感
    二次封装Response类
    视图与序列化传参
    Codeforces Round #523 (Div. 2) F. Katya and Segments Sets (交互题+思维)
    Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
    Codeforces 1060E(思维+贡献法)
    Codeforces Round #520 (Div. 2) E. Company(dfs序判断v是否在u的子树里+lca+线段树)
    Codeforces Round #513 by Barcelona Bootcamp C. Maximum Subrectangle(双指针+思维)
    Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)
  • 原文地址:https://www.cnblogs.com/wengzilin/p/2418136.html
Copyright © 2020-2023  润新知