IOS开发系列——UIView专题之五:常用开发技巧篇

5UIView开发技巧

5.1常用技巧

5.1.1使用半透明View与不透明SubView

半透明背景视图只能用此种方法设置颜色,否则subView也是半透明的。

blurView.backgroundColor= [UIColorcolorWithRed:0green:0blue:0alpha:0.3];

5.1.2[super layoutSubviews]要发到layoutSubviews方法末尾位置

在自定义子View中使用layoutSubviews时应注意,[superlayoutSubviews];最好放在方法默认最后执行,不然IOS7下面可能引起挂机。

5.1.3内容自适应属性UIViewContentMode

UIImageView的contentMode这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有以下几个常量可供设定:

UIViewContentModeScaleToFill

UIViewContentModeScaleAspectFit

UIViewContentModeScaleAspectFill

UIViewContentModeRedraw

UIViewContentModeCenter

UIViewContentModeTop

UIViewContentModeBottom

UIViewContentModeLeft

UIViewContentModeRight

UIViewContentModeTopLeft

UIViewContentModeTopRight

UIViewContentModeBottomLeft

UIViewContentModeBottomRight

注意以上几个常量,凡是没有带Scale的,当图片尺寸超过ImageView尺寸时,只有部分显示在ImageView中。UIViewContentModeScaleToFill属性会导致图片变形。UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。

5.1.4hitTest方法以及不规则区域内触摸事件处理方法

5.1.4.1hitTest:withEvent:方法流程

iOS系统检测到手指触摸(Touch)操作时会将其放入当前活动Application的事件队列,UIApplication会从事件队列中取出触摸事件并传递给key window(当前接收用户事件的窗口)处理,window对象首先会使用hitTest:withEvent:方法寻找此次Touch操作初始点所在的视图(View),即需要将触摸事件传递给其处理的视图,称之为hit-test view。

window对象会在首先在view hierarchy的顶级view上调用hitTest:withEvent:,此方法会在视图层级结构中的每个视图上调用pointInside:withEvent:,如果pointInside:withEvent:返回YES,则继续逐级调用,直到找到touch操作发生的位置,这个视图也就是hit-test view。

hitTest:withEvent:方法的处理流程如下:

•首先调用当前视图的pointInside:withEvent:方法判断触摸点是否在当前视图内;

•若返回NO,则hitTest:withEvent:返回nil;

•若返回YES,则向当前视图的所有子视图(subviews)发送hitTest:withEvent:消息,所有子视图的遍历顺序是从top到bottom,即从subviews数组的末尾向前遍历,直到有子视图返回非空对象或者全部子视图遍历完毕;

•若第一次有子视图返回非空对象,则hitTest:withEvent:方法返回此对象,处理结束;

•如所有子视图都返回非,则hitTest:withEvent:方法返回自身(self)。

hitTest:withEvent:方法忽略隐藏(hidden=YES)的视图,禁止用户操作(userInteractionEnabled=YES)的视图,以及alpha级别小于0.01(alpha<0.01)的视图。如果一个子视图的区域超过父视图的bound区域(父视图的clipsToBounds属性为NO,这样超过父视图bound区域的子视图内容也会显示),那么正常情况下对子视图在父视图之外区域的触摸操作不会被识别,因为父视图的pointInside:withEvent:方法会返回NO,这样就不会继续向下遍历子视图了。当然,也可以重写pointInside:withEvent:方法来处理这种情况。

对于每个触摸操作都会有一个UITouch对象,UITouch对象用来表示一个触摸操作,即一个手指在屏幕上按下、移动、离开的整个过程。UITouch对象在触摸操作的过程中在不断变化,所以在使用UITouch对象时,不能直接retain,而需要使用其他手段存储UITouch的内部信息。UITouch对象有一个view属性,表示此触摸操作初始发生所在的视图,即上面检测到的hit-test view,此属性在UITouch的生命周期不再改变,即使触摸操作后续移动到其他视图之上。

【原】ios的hitTest方法以及不规则区域内触摸事件处理方法

http://www.cnblogs.com/wengzilin/p/4249847.html

hitTest:withEvent:方法流程

http://blog.csdn.net/jiajiayouba/article/details/23447145

5.1.4.2使用hitTest自定义响应事件

1、hitTest Hacking the responder chain

在此例子中button,scrollview同为topView的子视图,但scrollview覆盖在button之上,这样在在button上的触摸操作返回的hit-test view为scrollview,button无法响应,可以修改topView的hitTest:withEvent:方法如下:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

UIView *result = [super hitTest:pointwithEvent:event];

CGPoint buttonPoint = [underButtonconvertPoint:point fromView:self];

if ([underButton pointInside:buttonPointwithEvent:event]) {

return underButton;

}

return result;

}

这样如果触摸点在button的范围内,返回hittestView为button,从button按钮可以响应点击事件。

2、Paging-enabled UIScrollView with Previews

BSPreviewScrollView

关于这两个例子,可以看之前文章的说明,见Paging-enabled

UIScrollView

5.1.5通过UIView对象获取其所属UIViewController

通过UIView对象获取该对象所属的UIViewController可以使用UIResponder的nextResponder方法获得,UIView类继承于UIResponder,因此可以直接使用。

根据文档描述,如果View有view controller,则通过nextResponder方法返回,如果没有则返回superview。

下面是英文原文:

if the viewhas a view controller, it is returned by nextResponder.

If there is noview controller, the method will return the superview

相关代码如下:遍历该View的树形结构,获取到其所属的ViewController

•- (UIViewController*)viewController {

•for(UIView* next = [self superview]; next; next = next.superview) {

•UIResponder* nextResponder = [next nextResponder];

•if([nextResponder isKindOfClass:[UIViewControllerclass]]) {

•return(UIViewController*)nextResponder;

•}

•}

•returnnil;

}

5.1.6坐标体系转换

•//将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值

•- (CGPoint)convertPoint:(CGPoint)pointtoView:(UIView*)view;

•//将像素point从view中转换到当前视图中,返回在当前视图中的像素值

•- (CGPoint)convertPoint:(CGPoint)pointfromView:(UIView*)view;

•//将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect

•- (CGRect)convertRect:(CGRect)recttoView:(UIView*)view;

•//将rect从view中转换到当前视图中,返回在当前视图中的rect

•- (CGRect)convertRect:(CGRect)rectfromView:(UIView*)view;

例把UITableViewCell中的subview(btn)的frame转换到controllerA中

1// controllerA中有一个UITableView, UITableView里有多行UITableVieCell,cell上放有一个button

2//在controllerA中实现:

3CGRect rc = [cellconvertRect:cell.btn.frametoView:self.view];

4或

5CGRect rc = [self.viewconvertRect:cell.btn.framefromView:cell];

6//此rc为btn在controllerA中的rect

7

8或当已知btn时:

9CGRect rc = [btn.superviewconvertRect:btn.frametoView:self.view];

10或

CGRect rc = [self.viewconvertRect:btn.framefromView:btn.superview];

6参考链接

iOS开发UI篇—UIWindow简单介绍

http://www.cnblogs.com/wendingding/p/3770052.html

iOS动画总结----UIView动画

http://blog.csdn.net/huifeidexin_1/article/details/7597868

UIView动画(过渡效果)的学习笔记

http://www.cnblogs.com/lovecode/archive/2011/11/05/2237077.html

动画UIView

animateWithDuration使用详解

http://blog.163.com/wzi_xiang/blog/static/65982961201211104227946/

UIView动画效果的四种调用方式

http://www.cnblogs.com/sell/archive/2013/02/09/2909522.html

(Good)iOS事件分发机制(一)hit-Testing

http://suenblog.duapp.com/blog/100031/iOS事件分发机制(一)%20hit-Testing

(Good)iOS事件分发机制(二)The Responder Chain

http://suenblog.duapp.com/blog/100032/iOS事件分发机制(二)The%20Responder%20Chain

hitTest:withEvent:方法流程

http://blog.csdn.net/jiajiayouba/article/details/23447145

iOS的UIView的hitTest的分析

http://blog.csdn.net/sanjunsheng/article/details/25080797

[IOS]hitTest的作用与用法【转】

http://blog.csdn.net/smking/article/details/9791365

iOS开发笔记--UIView中的坐标转换

http://blog.csdn.net/hopedark/article/details/18215083

IOS--UIView中的坐标转换

http://blog.sina.com.cn/s/blog_a573f7990102vgui.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江中散人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值