随意细解:UI -- 设计模式、手势识别

本文详细介绍了UI设计中的target/Action模式和代理模式,强调了这些模式的主要目标是解耦并提高代码复用性。同时,文章还探讨了在实现点击事件时如何遵循MVC设计模式。此外,文章涵盖了UIGestureRecognizer的各种手势识别,包括轻拍、长按、旋转、捏合、平移、轻扫和边缘扫等,展示了如何在实际应用中添加和响应这些手势。
摘要由CSDN通过智能技术生成

target/Action设计模式

举例:点击view实现点击功能,改变背景颜色(类似button的addtarget方法)

创建ButtonView,遵守协议:

在ButtonView.h中:为了方便调用,声明成属性

@property (nonatomic, retain) id target;
@property (nonatomic, assign) SEL action;
            // 初始化方法
- (instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action; 

在ButtonView.m中:

- (void)dealloc
{
    [self.target release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action
{
    self = [super initWithFrame:frame];
    if (self) {
     // 初始化时,对属性进行赋值
    self.target = target;
    self.action = action;

     }
    return self;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
        // 使用self.target对象调用 action方法
        // 让一个对象 去调用这个对象类里的方法
        // Object 可携带的参数
        [self.target performSelector:self.action withObject:self];
}

创建RootViewController作为根控制器

- (void)viewDidLoad {
    [super viewDidLoad];
    ButtonView *buttonView = [[ButtonView alloc]initWithFrame:CGRectMake(100, 100, 100, 100) target:self action:@selector(buttonClick:)];
    buttonView.backgroundColor = [UIColor redColor];
    [self.view addSubview:buttonView];
    [buttonView release];
}
- (void)buttonClick:(ButtonView *)buttonView
{
     buttonView.backgroundColor = [UIColor blackColor];
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值