几个经常需要自定义的组件: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

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并