项目中遇到的问题
1 push到一个新的控制器,页面类似新闻文章,用UITextView控件来初始化放置内容,第一次push的时候会发生卡顿,后面再push则一切正常。
解决办法 : 在首页界面viewDidLoad中添加代码
UITextView * textView = [[UITextView alloc]initWithFrame:CGRectZero];
[self.view addSubview:textView];
还不是从根本解决问题本质,希望能找到更好的方法。
-
oc项目中手势和系统菜单控制器的应用。
在开发中我们常用的手势一般有捏合 、点击、 长按、旋转、滑动,清扫几种,项目中刚好用到几种来分享记录下。
我抽了一点出来,这界面就录了一半进来。尴尬了。
文字正常.gif
- 点击手势 ,这个手势太常用了
/*! 点击 */
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[textView addGestureRecognizer:tap];
- 长按手势,项目中用于长按显示菜单控制器
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
longPress.delegate =self;
[self addGestureRecognizer:longPress];
设置代理是来解决长按和单击手势的冲突,遵循UIGestureRecognizerDelegate代理
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer{
NSLog(@"%@", NSStringFromClass([otherGestureRecognizer class]));
if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] && [NSStringFromClass([otherGestureRecognizer class])isEqualToString:@"UITapGestureRecognizer"]){
return NO;
}
return YES;
}
长按事件,使用菜单控制器
- (void)longPress:(UILongPressGestureRecognizer *)gestureRecognizer{
tag = gestureRecognizer.view.tag;
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
[gestureRecognizer.view becomeFirstResponder];
UIMenuController *menu = [UIMenuController sharedMenuController];
UIMenuItem *item1 = [[UIMenuItem alloc]initWithTitle:@"居左" action:@selector(left:)];
UIMenuItem *item2 = [[UIMenuItem alloc]initWithTitle:@"居中" action:@selector(center:)];
UIMenuItem *item3 = [[UIMenuItem alloc]initWithTitle:@"居右" action:@selector(right:)];
UIMenuItem *item4 = [[UIMenuItem alloc]initWithTitle:@"删除" action:@selector(myDelete:)];
menu.menuItems =@[item1,item2,item3,item4];
//菜单控制器的显示位置
[menu setTargetRect:gestureRecognizer.view.bounds inView:gestureRecognizer.view];
[menu setMenuVisible:YES animated:YES];
}
}
- 平移手势
/*! 平移 */
UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[textView addGestureRecognizer:panGes];
- (void)pan:(UIPanGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
gestureRecognizer.view.layer.borderColor = [UIColor blackColor].CGColor;
gestureRecognizer.view.layer.borderWidth = 1;
}if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
gestureRecognizer.view.layer.borderWidth = 0;
}
/*! 获取拖拽的偏移值 */
CGPoint point = [gestureRecognizer translationInView:self.view];
/*! 改变textview中心点 只允许上下滑动*/
gestureRecognizer.view.center = CGPointMake(gestureRecognizer.view.center.x+point.x, gestureRecognizer.view.center.y+point.y);
/*! 手势增量置为零*/
[gestureRecognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
- 旋转手势
/*! 旋转 */
UIRotationGestureRecognizer *rota = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
[textView addGestureRecognizer:rota];
/*! 旋转 */
- (void)rotation:(UIRotationGestureRecognizer *)gestureRecognizer{
//通过transform 进行旋转变换
_textView.transform = CGAffineTransformRotate(_textView.transform, gestureRecognizer.rotation);
//将旋转角度 置为 0
gestureRecognizer.rotation = 0;
}
- 缩放手势
/*! 缩放 */
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
[textView addGestureRecognizer:pinch];
/*! 缩放 捏合手势*/
- (void)pinch:(UIPinchGestureRecognizer *)gestureRecognizer{
_currentScale = gestureRecognizer.scale * _currentScale;
/*! 开始缩放 */
if (gestureRecognizer.state ==UIGestureRecognizerStateBegan) {
gestureRecognizer.view.layer.borderColor = [UIColor whiteColor].CGColor;
gestureRecognizer.view.layer.borderWidth = 1;
}
if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
_textView.font = [UIFont systemFontOfSize:17+(_currentScale-1)*20];
CGSize size ;
size = [_textView.text boundingRectWithSize:CGSizeMake(kScreenWidth-40, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :_textView.font } context:nil].size;
_textView.bounds = CGRectMake(0, 0,size.width+20, size.height+20);
}
if (gestureRecognizer.state==UIGestureRecognizerStateEnded) {
gestureRecognizer.view.layer.borderWidth = 0;
}
//设置比例 为 1 。下次在这个scale基础上改变
gestureRecognizer.scale = 1;
}
这里要注意的是,网上的博客上很多简单介绍使用是下面这样
//缩放
-(void)doPinch:(UIPinchGestureRecognizer *)sender{
imageView.transform = CGAffineTransformScale(imageView.transform, sender.scale, sender.scale);
sender.scale = 1;
}
但是这样写会导致文字或者图像的失真,变得模糊。
像这样
正常情况
文字正常.gif- 菜单控制器
注意事项 :
- 需要自定义控件并实现下面两个方法.
//是否可以成为第一相应
-(BOOL)canBecomeFirstResponder{
return YES;
}
//是否可以接收某些菜单的某些交互操作
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
return YES;
}
2 代码将控件设置第一响应者
[self becomeFirstResponder];
3 设置管理控制器的位置和显示
//设置菜单显示的位置 frame inView设置其所在的视图
[menu setTargetRect:gestureRecognizer.view.bounds inView:gestureRecognizer.view];
//将菜单控件设置为可见
[menu setMenuVisible:YES animated:YES];
结束后就可以正确显示 。
测试小demo demo
学习到的新东西
- 给按钮添加block属性
- 关联对象的key平常都是static const void *buttonKey = @"buttonKey"; 当成一个类型常量来写。 现在发现可以直接使用@selector()方式来当作key,简单方便不会忘记.
- (ZDButton_block)ZDButton_block{
return objc_getAssociatedObject(self, @selector(ZDButton_block));
}
- (void)setZDButton_block:(ZDButton_block)ZDButton_block{
objc_setAssociatedObject(self, @selector(ZDButton_block), ZDButton_block, OBJC_ASSOCIATION_COPY);
}
网友评论