iOS_小蟹专题iOS基础首页投稿(暂停使用,暂停投稿)

【View层】IOS纯代码绘制界面(一)

2016-06-07  本文已影响2270人  纳达丶无忌

IOS开发中界面绘制占据了绝大部分的工作量,当前可以使用StoryBoard,xib以及代码三种方式定制所需要的界面。在这三种方式中代码是最灵活,最具有扩展性的,而特别是多人合作的大项目中StoryBoard,xib不利于代码合并。
接下来对之前项目的的界面开发做一些总结。

UIViewController和重要子类

UINavigationController
打交道最多的类无疑是这两个,一般我们采用UINavigationController(导航控制器,navi)作为UIViewController(vc)的容器,最常用的方法是

//用指定的vc创建,该vc位于栈底。
-(instancetype)initWithRootViewController:(UIViewController *)rootViewController; 

//向栈里push一个vc,位于栈顶
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

//从栈里pop一个vc。
-(nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;

一般app中只需要用到一个navi,在AppDelegate中完成初始化即可,后续的界面迁移都是push和pop。值得注意的是使用了navi之后,每个界面会产生一个navigationBar,即位于界面顶部的导航条。导航条的作用是显示当前的界面title和返回按钮,并支持自己添加需要的辅助功能。在任何vc中可以通过

self.navigationController.navigationBar或者self.navigationItem

访问这个组件,如设置背景色,添加按钮等:

[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:19], NSForegroundColorAttributeName : someColor}];
[self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];
UIBarButtonItem  * activityDetail = [[UIBarButtonItem alloc] initWithTitle:@“帮助” style:UIBarButtonItemStylePlain target:self action:@selector(helpButtonClicked)];
self.navigationItem.rightBarButtonItem = activityDetail;
UIViewController

作为视图控制器基类,我们一般在里面添加view代码,显示逻辑,包括按钮事件、代理方法(比如tableView的代理等)。不注意的话vc会变得很大,如果行数过多则不易维护,所以说复杂的页面应该做架构上的区分,如MVVM模式将view,viewModel抽离出来。这里暂不细讲。
vc最重要的方法包括:

生命周期相关的方法

//重写该方法意味着不加载外部的xib文件,自己绘制界面
-(void)loadView; 
-(void)viewDidLoad; 
-(void)viewWillAppear:(BOOL)animated;   
-(void)viewDidAppear:(BOOL)animated;     

以上方法在view加载过程中依次调用。一般在loadView或者viewDidLoad中添加自定义的view,但不能添加过于耗时的操作,以避免界面加载被阻塞导致的卡顿。一般把如网络操作,数据库操作加载viewDidAppear中,界面加载可见之后执行。

UIView和重要子类

UIView

先总结UIView的通用方法和属性,然后介绍常用view控件的使用经验。
初始化方法,以一个frame作为参数。

基本

位置与空间相关:

每个UIView有frame 和bounds两个属性,分别代表相对父容器的矩形和相对本容器的矩形(有点难理解)

struct CGRect { 
  CGPoint origin; 
  CGSize size; 
};

bound一般用来修改view的相对位置,类似于css中的相对布局,frame类似绝对布局。如图

Paste_Image.png

view的层次

view的绘制

- (void)drawRect:(CGRect)rect {  
    // Drawing code.  
    //获得处理的上下文    
    CGContextRef context = UIGraphicsGetCurrentContext();    
    //设置线条样式    
    CGContextSetLineCap(context, kCGLineCapSquare);     
    //设置线条粗细宽度    
    CGContextSetLineWidth(context, 1.0);     

    //设置颜色    
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);     
    //开始一个起始路径    
    CGContextBeginPath(context);     
    //起始点设置为(0,0):注意这是上下文对应区域中的相对坐标,    
    CGContextMoveToPoint(context, 0, 0);     
    //设置下一个坐标点    
    CGContextAddLineToPoint(context, 100, 100);     
    //设置下一个坐标点    
    CGContextAddLineToPoint(context, 0, 150);    
    //设置下一个坐标点    
    CGContextAddLineToPoint(context, 50, 180);    
    //连接上面定义的坐标点    
    CGContextStrokePath(context);  
 }  

调用时机drawRect是在Controller.loadView,viewDidLoad 两方法之后掉用的.所以不用担心在控制器中,这些View的drawRect就开始画了.这样可以在控制器中设置一些值给View(如果这些View draw的时候需要用到某些变量值).

Paste_Image.png

动画相关

- +(void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^ )(void))animations completion:(void (^ __nullable)(BOOL finished))completion

方法接受几个参数:duration, delay, options, animations, completion。duration代表执行多久delay代表延迟多少时间开始执行,option参考UIViewAnimationOptions(篇幅过大此处略过),animation和completion各为一个block,代表执行的动画和完成后执行的代码。一段示例代码(慢慢放大后迅速缩小的提示效果):

Paste_Image.png

其他方法和该方法类似,实际使用时根据实际情况决定。

手势相关
添加和删除手势,不过多介绍。

UIControl

子类包括:


Paste_Image.png

其中UIButton和UITextField,UISwitch都是平时十分常用的控件,作为他们的基类有必要介绍一下。

重要的属性与方法:

UIControl 基本的 State 变化过程如下
1.什么都没干的时候:Normal
2.当你的手指按下去,还没放的时候:Highlighted
3.当手指放开的时候:如果这个 UIControl 有 Selected 状态的话,就会变成: Selected
再重复上述过程一次,就会从 Selected->Highlighted-> Normal
但是普通的 UIButton 这个 UIControl 的 subclass,是没有 Selected 状态的,它就只有 Normal 和 Highlighted,只会在这两个状态间切换。(正常情况下,如果你设置了 disable 的话,还会变到 Disabled)

按钮事件

- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; 

普通的按钮通过该方法绑定事件即可,其中sel是一个方法指针,UIControlEvents定义了可发送事件的几种类型。按钮点击一般使用UIControlEventTouchUpInside。代表在控件范围内按下去并放开。其余的属性可参考UIControlEvents。

自定义事件实现

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;
- (void)endTrackingWithTouch:(nullable UITouch *)touch withEvent:(nullable UIEvent *)event;

通过以上三个方法可以实现自定义的控件,比如要自己实现滑块效果,先调用begin,然后在continue中利用touch.locationInView的值来改变滑块代表的数值,在end中完成网络,数据库等操作。

上一篇 下一篇

猜你喜欢

热点阅读