按钮在执行frame动画的时候怎么响应触发事件?

简介:

按钮在执行frame动画的时候怎么响应触发事件?

代码中效果(请注意,我并没有点击到按钮,而是点击到按钮的终点frame值处):

对应的代码:

//
//  ViewController.m
//  TapButton
//
//  Created by YouXianMing on 14/12/7.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 初始化按钮
    UIButton *button       = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self
               action:@selector(buttonEvent:)
     forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    // 执行动画
    [UIView animateWithDuration:10.f
                          delay:0
                        options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
        button.frame = CGRectMake(0, 468, 100, 100);
    } completion:^(BOOL finished) {
        
    }];
}

/**
 *  按钮事件
 *
 *  @param button 按钮事件
 */
- (void)buttonEvent:(UIButton *)button {
    NSLog(@"YouXianMing");
}

@end

修改过后的效果:

源码:

//
//  ViewController.m
//  TapButton
//
//  Created by YouXianMing on 14/12/7.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "ChildView.h"

@interface ViewController ()

{

    ChildView *tmpView;

}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 初始化按钮
    tmpView                        = [[ChildView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    tmpView.backgroundColor        = [UIColor redColor];
    tmpView.userInteractionEnabled = NO; // 让self.view获取点击事件(穿透自身)
    [self.view addSubview:tmpView];

    
    // 执行动画
    [UIView animateWithDuration:10.f
                          delay:0
                        options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
        tmpView.frame = CGRectMake(0, 468, 100, 100);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // 获取点击点
    CGPoint point = [[touches anyObject] locationInView:self.view];
    
    // 获取tmpView的layer当前的位置
    CGPoint presentationPosition = [[tmpView.layer presentationLayer] position];
    
    // 判断位置,让tmpView接受点击事件
    if (point.x > presentationPosition.x - 50 && point.x < presentationPosition.x + 50 &&
        point.y > presentationPosition.y - 50 && point.y < presentationPosition.y + 50) {
        [tmpView touchesBegan:touches withEvent:event];
    }
}


@end

ChildView.h 与 ChildView.m
//
//  ChildView.h
//  TapButton
//
//  Created by YouXianMing on 14/12/7.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ChildView : UIView

@end


//
//  ChildView.m
//  TapButton
//
//  Created by YouXianMing on 14/12/7.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ChildView.h"

@implementation ChildView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"获取点击事件");
}

@end

关键性的两步:

目录
相关文章
|
22天前
|
小程序 UED 开发者
小程序如何监听页面的滚动事件
小程序如何监听页面的滚动事件
21 0
|
7月前
如何让touchmove之后不触发touchend的事件
如何让touchmove之后不触发touchend的事件
38 0
|
4月前
[Qt5] 鼠标响应事件和计时器事件
[Qt5] 鼠标响应事件和计时器事件
24 0
|
JavaScript
使用jquery中实现按回车触发按钮事件和点击提交按钮触发的是一个事件
使用jquery中实现按回车触发按钮事件和点击提交按钮触发的是一个事件
108 0
|
图形学
Unity碰撞事件和触发事件
大家在刚开始接触Unity的时候,一定要理解碰撞与触发
426 0
Unity碰撞事件和触发事件
|
容器 C#
3D场景中的鼠标响应事件
原文:3D场景中的鼠标响应事件 今天要讲的是3D场景中的鼠标响应事件的处理,首先Button的响应是大家熟知的,只要加上一个click事件,然后写一个响应的处理时间就行了。对于二维平面上的一些控件也很好处理,比如在跳棋那篇文章中,就是用了ellipse的鼠标左右键按下的事件响应函数。
893 0

热门文章

最新文章