zoukankan      html  css  js  c++  java
  • 【转】几个经常需要自定义的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];

    }

    不能上传附件,有需要的可以联系我
  • 相关阅读:
    免费公共dns推荐
    vm10 mac 序列号
    sublimetext 序列号
    操作系统第6次实验报告:使用信号量解决进程互斥访问
    操作系统第5次实验报告:内存管理
    操作系统第4次实验报告:文件系统
    操作系统第3次实验报告:管道
    操作系统第2次实验报告:创建进程
    OS第1次实验报告:熟悉使用Linux命令和剖析ps命令
    第四次实验报告:使用Packet Tracer理解RIP路由协议
  • 原文地址:https://www.cnblogs.com/wengzilin/p/2418136.html
Copyright © 2011-2022 走看看