注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Tolecen's Blog

One Heart, One World

 
 
 
 
 

日志

 
 

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

2012-04-05 13:22:09|  分类: iOS/Objective-C |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

转自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];

}



//父视图是否可以将消息传递给子视图,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];

}

  评论这张
 
阅读(1097)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017