20150702_UI之UIView中层的注意事项及MVC练习

本文深入探讨了iOS应用开发中Objective-C语言的核心用法,包括面向对象编程、类与对象、消息传递、属性与方法的定义与使用,以及如何利用Objective-C构建高效、稳定的iOS应用。

代码实例:

//
//  ViewController.m
//  IOS150702_UI层的说明
//
//  Created by PengJunlong on 15/7/2.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 70, 70)];
    topView.backgroundColor = [UIColor greenColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem ];
    btn.frame = CGRectMake(30, 30, 100, 100);
    btn.backgroundColor = [UIColor blueColor];
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(changeViewColor) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:btn];
    [self.view addSubview:topView];
}

- (void)changeViewColor
{
    self.view.backgroundColor = [UIColor redColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
结果:



蓝色的button是放在绿色的UIView上的,且Button的大小已经超出UIView的范围,点击绿色范围之外的部分,按钮没有任何反应,因为该部分已经超出范围.点击绿色和蓝色交叉的部分时会相应按钮的事件,修改View的背景色为红色



UIView的MVC模式练习:

点击下一条会随机改变条形的长度,使用MVC模式实现


//
//  View.h
//  IOS150701_UI_条形图MVC练习
//
//  Created by PengJunlong on 15/7/2.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <UIKit/UIKit.h>
@interface View : UIView
- (id)initWithFrame:(CGRect)frame addTarget:(id)target action:(SEL)action;
- (void)UpdateViewByArray:(NSArray *)arr;
@end

//
//  View.m
//  IOS150701_UI_条形图MVC练习
//
//  Created by PengJunlong on 15/7/2.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "View.h"
@implementation View

- (id)initWithFrame:(CGRect)frame addTarget:(id)target action:(SEL)action
{
    if (self = [super initWithFrame:frame]) //不能使用init,使用init初始化时,得到的self大小是{(0.0),(0,0)};
                                            //添加的子视图超出父视图,点击超出范围的部分则没有任何反应,点击父视图范围内的则有反应
    {
        UIView *bgview = [[UIView alloc] initWithFrame:frame];
        bgview.backgroundColor = [UIColor cyanColor];
        bgview.tag = 100;
        CGFloat size = (frame.size.height-80)/12;
        for (int i=0; i<12; i++)
        {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, i*size+20, 50, size-10)];
            label.text = [NSString stringWithFormat:@"%d",i+1];
            label.backgroundColor = [UIColor grayColor];
            label.alpha = 0.7;
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor redColor];
            [bgview addSubview:label];
            
            UIView *stickView = [[UIView alloc] initWithFrame:CGRectMake(50, i*size+20, 0, size-10)];
            stickView.backgroundColor = [UIColor blueColor];
            stickView.tag = i+1;
            [bgview addSubview:stickView];
            
        }
        UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
        nextButton.frame = CGRectMake(50, frame.size.height-50, frame.size.width-100, 30);
        [nextButton setTitle:@"下一条" forState:UIControlStateNormal];
        nextButton.backgroundColor = [UIColor grayColor];
        [nextButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        [bgview addSubview:nextButton];
        [self addSubview:bgview];
    }
    
    return self;
}

- (void)UpdateViewByArray:(NSArray *)arr
{
    
    UIView *bgView = (UIView *)[self viewWithTag:100];
    for ( int i=0; i<12; i++)
    {
        UIView *view = [bgView viewWithTag:i+1];
        CGRect frame = view.frame;
        frame.size.width = [[arr objectAtIndex:i] intValue];
        view.frame = frame;
    }
}
@end

MVC中的Model:
//
//  DataModel.h
//  IOS150701_UI_条形图MVC练习
//
//  Created by PengJunlong on 15/7/2.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface DataModel : NSObject
{
    NSMutableArray *_dataArray;
}

- (id)init;
- (NSArray *)updateModel;
@end

//
//  DataModel.m
//  IOS150701_UI_条形图MVC练习
//
//  Created by PengJunlong on 15/7/2.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "DataModel.h"

@implementation DataModel
- (id)init
{
    if (self = [super init])
    {
        _dataArray = [NSMutableArray array];
        for (int i=0; i<12; i++)
        {
            [_dataArray addObject:[NSNumber numberWithInt:0]];
        }
    }
    return self;
}

- (NSArray *)updateModel
{
    for (int i=0; i<12; i++)
    {
        NSNumber *number = [NSNumber numberWithInt:arc4random()%300];
        [_dataArray replaceObjectAtIndex:i withObject:number];
    }
    return _dataArray;
}

@end

MVC中的Controller:

//
//  ViewController.h
//  IOS150701_UI_条形图MVC练习
//
//  Created by PengJunlong on 15/7/2.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

//
//  ViewController.m
//  IOS150701_UI_条形图MVC练习
//
//  Created by PengJunlong on 15/7/2.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "ViewController.h"
#import "View.h"
#import "DataModel.h"
@interface ViewController ()
{
    View *_view;
    DataModel *_datamodel;
}
@end

//MVC设计模式
//Model/View/Controller

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createModel];
    [self createUI];
    [self btnRefrehsView];
}

- (void)createModel
{
    _datamodel = [[DataModel alloc] init];
}

- (void)createUI
{
    CGRect frame = self.view.frame;
    frame.size.height = self.view.frame.size.height-80;
    _view = [[View alloc] initWithFrame:frame addTarget:self action:@selector(btnRefrehsView)];
    [self.view addSubview:_view];
}

- (void)btnRefrehsView
{
    NSLog(@"点击");
    [_view UpdateViewByArray:[_datamodel updateModel]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


### 代码概述 该函数 `ProcessListLiveItemClicked` 用于处理实时消息项的点击事件,根据是否支持折叠功能决定如何响应点击。 --- ### 代码解析 1. **判断是否启用实时消息功能(`CONFIG_UI_LIVE_MSG`)** - 如果启用了此功能,函数才会被编译和使用。 2. **判断是否支持折叠功能(`CONFIG_UI_LIVE_MSG_NOFOLD`)** - `#if !(defined(CONFIG_UI_LIVE_MSG_NOFOLD))` 表示如果不定义 `CONFIG_UI_LIVE_MSG_NOFOLD`,即支持折叠。 #### 分支一:支持折叠逻辑 ```cpp if ((liveItems != nullptr) && (&view == static_cast<UIView *>(liveItems))) { CardEnterTransition(); ListLiveItemClicked(); return true; } ``` - 判断点击的 `view` 是否为 `liveItems`。 - 如果是,执行页面跳转动画 `CardEnterTransition()`,并调用 `ListLiveItemClicked()` 处理点击逻辑。 - 返回 `true` 表示事件已被处理。 #### 分支二:不支持折叠逻辑(多个实时项) ```cpp for (int i = INDEX_ZERO; i < LIVE_INFORM_NUM_OF_LIST_ITEM; i++) { if ((liveItems[i] == nullptr) || (&view != static_cast<UIView *>(liveItems[i]))) { continue; } ListLiveItemClicked(i); return true; } ``` - 遍历所有 `liveItems[i]`,查找与点击对象匹配的项。 - 找到后调用 `ListLiveItemClicked(i)` 并返回 `true`,表示点击处理完成。 3. **若未找到匹配项,则返回 `false`** - 表示该点击事件不是针对实时消息项。 --- ### 知识点 - **条件编译控制功能分支**:通过宏定义选择性地启用或关闭折叠功能逻辑。 - **页面跳转动画处理**:使用 `CardEnterTransition()` 实现页面切换的视觉效果。 - **事件匹配与处理机制**:通过指针或遍历判断点击对象,确保事件准确响应。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值