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

本文主要是介绍几个经常需要自定义的组件:UIScrollview、UItextView、UIButton,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

}

这篇关于几个经常需要自定义的组件:UIScrollview、UItextView、UIButton的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/688362

相关文章

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

C++ RabbitMq消息队列组件详解

《C++RabbitMq消息队列组件详解》:本文主要介绍C++RabbitMq消息队列组件的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. RabbitMq介绍2. 安装RabbitMQ3. 安装 RabbitMQ 的 C++客户端库4. A

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

PyQt6中QMainWindow组件的使用详解

《PyQt6中QMainWindow组件的使用详解》QMainWindow是PyQt6中用于构建桌面应用程序的基础组件,本文主要介绍了PyQt6中QMainWindow组件的使用,具有一定的参考价值,... 目录1. QMainWindow 组php件概述2. 使用 QMainWindow3. QMainW

使用easy connect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题

《使用easyconnect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题》:本文主要介绍使用easyconnect之后,maven无法... 目录使用easGWowCy connect之后,maven无法使用,原来需要配置-DJava.net.pr

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

SpringQuartz定时任务核心组件JobDetail与Trigger配置

《SpringQuartz定时任务核心组件JobDetail与Trigger配置》Spring框架与Quartz调度器的集成提供了强大而灵活的定时任务解决方案,本文主要介绍了SpringQuartz定... 目录引言一、Spring Quartz基础架构1.1 核心组件概述1.2 Spring集成优势二、J