button is not in the parent attempt, but needs to respond to click events

Source: Internet
Author: User
Tags uikit

A Click event cannot be responded to when the button is not in the scope of the parent attempt. The project involved this part, then wrote a small demo (click the button, pop up two button, and all can respond to the Click event), as follows:

Custom View:cumoreview

//
CUMoreView.h
//

#import <UIKit/UIKit.h>

typedef void (^btnclickblock) (UIButton *btn);

@interface Cumoreview:uiview

@property (nonatomic, copy) Btnclickblock Btnclock;

-(void) Didclickbuttonwithblock: (Btnclickblock) Btnclock;

@end

//
cumoreview.m
Animation
//

#import "CUMoreView.h"
#import "Uiview+cu.h"

@interface Cumoreview ()
@property (nonatomic, weak) UIButton *btn2;
@property (nonatomic, weak) UIButton *btn3;
@property (nonatomic, assign) BOOL isshow;
@end

@implementation Cumoreview

-(Instancetype) initWithFrame: (CGRect) Frame {
self = [super Initwithframe:frame];
Self.isshow = NO;
UIButton *btn1 = [[UIButton alloc]initwithframe:cgrectmake (0, 0, 60, 30)];
Btn1.backgroundcolor = [Uicolor Lightgraycolor];
[Btn1 settitle:@ "AAAA" forstate:uicontrolstatenormal];
Btn1.tag = 10001;
[Btn1 addtarget:self Action: @selector (Didclickbutton:) forcontrolevents:uicontroleventtouchupinside];
[Self addsubview:btn1];

UIButton *btn2 = [[UIButton alloc]initwithframe:cgrectmake (0,-30, 60, 30)];
Btn2.backgroundcolor = [Uicolor Lightgraycolor];
[Btn2 settitle:@ "bbbb" forstate:uicontrolstatenormal];
btn2.userinteractionenabled = YES;
Btn2.alpha = 1;
Btn2.tag = 10002;
[Btn2 addtarget:self Action: @selector (Didclickbutton:) forcontrolevents:uicontroleventtouchupinside];
SELF.BTN2 = BTN2;
[Self addsubview:btn2];

UIButton *btn3 = [[UIButton alloc]initwithframe:cgrectmake (0,-60, 60, 30)];
Btn3.backgroundcolor = [Uicolor Lightgraycolor];
[Btn3 settitle:@ "CCCC" forstate:uicontrolstatenormal];
Btn3.alpha = 1;
Btn3.tag = 10003;
[Btn3 addtarget:self Action: @selector (Didclickbutton:) forcontrolevents:uicontroleventtouchupinside];
Self.btn3 = Btn3;
[Self addsubview:btn3];
return self;
}

-(void) Didclickbutton: (UIButton *) Clickbutton {
Nsinteger tag = Clickbutton.tag;

if (tag = = 10001) {

if (self.isshow = = NO) {//expand

[UIView animatewithduration:0.25 animations:^{
SELF.BTN2.Y =-30;
SELF.BTN3.Y =-60;
Self.btn2.alpha = 1;
Self.btn3.alpha = 1;
Self.isshow = YES;
}];
}else{//Shrinkage
[UIView animatewithduration:0.25 animations:^{
SELF.BTN3.Y = 0;
Self.btn3.alpha = 0;
self.btn2.y = 0;
Self.btn2.alpha = 0;
Self.isshow = NO;
}];
}
}

else {

if (Self.btnclock) {
Self.btnclock (Clickbutton);
}
}
}

-(void) Didclickbuttonwithblock: (Btnclickblock) Btnclock {
if (Btnclock) {
Self.btnclock = Btnclock;
}

}

-(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) Event {
UIView *view = [Super Hittest:point withevent:event];
if (view = = nil) {
Cgpoint tempoint = [self.btn2 convertpoint:point fromview:self];
Cgpoint Tempoint2 = [Self.btn3 convertpoint:point fromview:self];

if (Cgrectcontainspoint (Self.btn2.bounds, Tempoint))
{
view = SELF.BTN2;
}

if (Cgrectcontainspoint (Self.btn3.bounds, Tempoint2)) {
view = Self.btn3;
}

}
return view;
}

@end

//
Viewcontroller.m
//

#import "ViewController.h"
#import "CUMoreView.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];

Cumoreview *moreview = [[Cumoreview alloc]initwithframe:cgrectmake (200, 200, 60, 120)];
[Moreview didclickbuttonwithblock:^ (UIButton *btn) {
if (Btn.tag = = 10002) {

NSLog (@ "Click the middle button");

}else if (Btn.tag = = 10003) {
NSLog (@ "Click on the button above");
}
}];

[Self.view Addsubview:moreview];


}

-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}

@end

button is not in the parent attempt, but needs to respond to click events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.