前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UITextView 手势触发 TouchesBegan 函数

UITextView 手势触发 TouchesBegan 函数

作者头像
全栈程序员站长
发布2022-09-16 21:01:56
1.1K0
发布2022-09-16 21:01:56
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君

前几天做了个手势可以改变文章字体大小的功能。开始,在当前view中添加一个UITextView ,然后添加- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event函数,可怎么也触发不了,在网上找了些资料,说得也不是很清楚,今天把它总结下。

首先说原因吧,你把UITextView 加载到当前view上,然后在当前文件中写函数(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event,手势触摸时,其实触发的是当前view重构父类的touchesbegan函数,而加载UITextView时,UITextView 其实也有相应的touchesbegan函数,UITextView 继承UIScrollView->UIRespone 。所以说,当你点击UITextView想触发相应手势函数,是做不到了,因为它始终触发的是当前view的手势函数,明白了吧,现在来说做法。

关键步骤:重构UITextView

1、首先你得重现写个类,如MyTextView

#import <UIKit/UIKit.h>

@interface MyTextView : UITextView @end

#import “MyTextView.h”

@implementation MyTextView

– (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { } return self; }

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { }

– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { }

– (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { }

– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { }

@end

2、然后你再在view中添加改UITextView 如:

MyTextView *textView = [[MyTextView alloc] initWithFrame:CGRectMake(0, 63, 320, [SettingManager shareInstance].nIphoneHeight – TITLE_HEIGHT – NAV_HEIGHT*2)]; [self.view addSubview:textView];

这样当你手势触发的时候,就会触发MytextView 中的touchesBegan 函数了,再在相应的手势函数中就可以做相应的操作了。

最后,如果想要通过手势改变文章字体,图片等,如果文章较长,可能会先会滚动,从而忽略掉手势操作。那你就需要设置下了将canCanelContentTouches 设置为NO, 多点触发multipleTouchEnabled设置为YES,delaysContentTouches设置为NO,后两个必须设置。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/164184.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月25日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档