几个经常需要自定义的组件:UIScrollview、UItextView、UIButton

http://blog.csdn.net/yanxiaoqing/article/details/7108920

为了独立出组件的一些功能,如,为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];

}


[plain]  view plain copy
  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,.61.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];

}

不能上传附件,有需要的可以联系我

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值