自定义控键
Posted I‘m丶sure
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义控键相关的知识,希望对你有一定的参考价值。
1、自定义View
事件和响应者链
// ViewController.h
// 自定义view
//
// Created by tlh on 16/3/10.
// Copyright © 2016年 lamco. All rights reserved.
//
//事件和响应者链
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(strong,nonatomic)UILabel *lbltest;
@property(strong,nonatomic)UIButton *btn;
@property(strong,nonatomic)UIView *view1;
@property(strong,nonatomic)UIView *view2;
// 自定义view
//
// Created by tlh on 16/3/10.
// Copyright © 2016年 lamco. All rights reserved.
//
//事件和响应者链
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(strong,nonatomic)UILabel *lbltest;
@property(strong,nonatomic)UIButton *btn;
@property(strong,nonatomic)UIView *view1;
@property(strong,nonatomic)UIView *view2;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.lbltest=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
self.lbltest.backgroundColor=[UIColor grayColor];
[email protected]"test";
[self.view addSubview:self.lbltest];
self.btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
self.btn.frame=CGRectMake(100, 100, 50, 50);
[self.btn setTitle:@"TEST" forState:UIControlStateNormal];
[self.btn addTarget:self action:@selector(Testbtn) forControlEvents:UIControlEventTouchUpInside];
[self.lbltest addSubview:self.btn];
// 是否与用户交互 label默认是不交互。yes。
self.lbltest.userInteractionEnabled=YES;
self.view1=[[UIView alloc]initWithFrame:CGRectMake(100, 350, 200, 200)];
self.view1.backgroundColor=[UIColor redColor];
self.view2=[[UIView alloc]initWithFrame:CGRectMake(50,50, 50, 50)];
self.view2.backgroundColor=[UIColor greenColor];
[self.view1 addSubview:self.view2];
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.lbltest=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
self.lbltest.backgroundColor=[UIColor grayColor];
[email protected]"test";
[self.view addSubview:self.lbltest];
self.btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
self.btn.frame=CGRectMake(100, 100, 50, 50);
[self.btn setTitle:@"TEST" forState:UIControlStateNormal];
[self.btn addTarget:self action:@selector(Testbtn) forControlEvents:UIControlEventTouchUpInside];
[self.lbltest addSubview:self.btn];
// 是否与用户交互 label默认是不交互。yes。
self.lbltest.userInteractionEnabled=YES;
self.view1=[[UIView alloc]initWithFrame:CGRectMake(100, 350, 200, 200)];
self.view1.backgroundColor=[UIColor redColor];
self.view2=[[UIView alloc]initWithFrame:CGRectMake(50,50, 50, 50)];
self.view2.backgroundColor=[UIColor greenColor];
[self.view1 addSubview:self.view2];
[self.view addSubview:self.view1];
}
-(void)Testbtn
{
NSLog(@"btn");
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"begin");
// v1
NSLog(@"%@",[self.view2 nextResponder]);
// view
NSLog(@"%@",[[self.view2 nextResponder] nextResponder]);
// window 容器
NSLog(@"%@",[[[self.view2 nextResponder] nextResponder] nextResponder]);
//uiapplication 类
NSLog(@"%@",[[[[self.view2 nextResponder] nextResponder] nextResponder] nextResponder]);
// Appdelegate 协议
NSLog(@"%@",[[[[[self.view2 nextResponder] nextResponder] nextResponder] nextResponder] nextResponder]);
// null 再往下为空
{
NSLog(@"btn");
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"begin");
// v1
NSLog(@"%@",[self.view2 nextResponder]);
// view
NSLog(@"%@",[[self.view2 nextResponder] nextResponder]);
// window 容器
NSLog(@"%@",[[[self.view2 nextResponder] nextResponder] nextResponder]);
//uiapplication 类
NSLog(@"%@",[[[[self.view2 nextResponder] nextResponder] nextResponder] nextResponder]);
// Appdelegate 协议
NSLog(@"%@",[[[[[self.view2 nextResponder] nextResponder] nextResponder] nextResponder] nextResponder]);
// null 再往下为空
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"end");
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"move");
{
NSLog(@"end");
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"move");
}
以上是关于自定义控键的主要内容,如果未能解决你的问题,请参考以下文章