关于移动中的button的响应问题

来源:互联网 发布:知乎如何使用邮箱注册 编辑:程序博客网 时间:2024/05/22 00:09

在ios中 如果动画效果中包含的button的改变 ,比如移动,这时候button的点击事件是不会响应的,如果想要时间响应 有两种办法


1.使用定时器微分的方法,比较消耗内存,这里才用0.01s做为时间间隔

#import "ViewController.h"

#import <QuartzCore/QuartzCore.h>


#define which 2

#define number 4

@implementation ViewController

{

    UIView *primaryView;

    UIView *secondaryView;

    UIButton *_btnMove;

    NSTimer *_timer;

}


@synthesize displayingPrimary;


- (void) viewDidLoad {

    [superviewDidLoad];


    [selfcreateMoveButton];


}



/**

 *  移动动画且button移动中还可以交互

 */

- (void)createMoveButton

{

    _btnMove = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [_btnMoveaddTarget:selfaction:@selector(btnMoveDidSelect)forControlEvents:UIControlEventTouchUpInside];

    _btnMove.frame =CGRectMake(0,0,80, 40);

    [_btnMovesetTitle:@"touch me!"forState:UIControlStateNormal];

    [self.view addSubview:_btnMove];

    

    _timer = [NSTimerscheduledTimerWithTimeInterval:0.01ftarget:selfselector:@selector(buttonMoved)userInfo:nilrepeats:YES];


}


- (void)buttonMoved

{

    if(_btnMove.frame.origin.x !=250)

    {

        [UIViewbeginAnimations:nilcontext:nil];

        [UIViewsetAnimationDuration:0.01f];

        [UIViewsetAnimationCurve:UIViewAnimationCurveEaseOut];

        [_btnMovesetFrame:CGRectMake(_btnMove.frame.origin.x+1,_btnMove.frame.origin.y,_btnMove.frame.size.width,_btnMove.frame.size.height)];

        [UIViewcommitAnimations];

    }else{

        [_timer invalidate];//关闭定时器

    }

}


- (void)btnMoveDidSelect

{

    NSLog(@"tap!");

}



2.使用ios动画过程中的 图层触碰测试


自定义一个Myview


#import "MyView.h"

#import <QuartzCore/QuartzCore.h>


@implementation MyView {

    IBOutlet UIView* v;

}


- (void) awakeFromNib {

    [superawakeFromNib];

    UITapGestureRecognizer* t = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap:)];

    [vaddGestureRecognizer:t];

    t.cancelsTouchesInView =NO;

}


- (void) tap: (UIGestureRecognizer*) g {

    NSLog(@"tap! (gesture recognizer)");

}


- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    //这里对触碰测试做判断

    // v 是参与动画的子视图

    CALayer* lay = [v.layerpresentationLayer]; //获取视图的展示层

    CALayer* hitLayer = [lay hitTest: point];

    if (hitLayer == lay || hitLayer.superlayer == lay)

        return v;

    UIView* hitView = [superhitTest:pointwithEvent:event];

    if (hitView == v)

        return self;

    return hitView;

}


然后再viewcontroller的xib里 将self.view 设置为 自定义的Myview

.h文件

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController


@end




.m文件

#import "ViewController.h"

#import <QuartzCore/QuartzCore.h>


@implementation ViewController {

    IBOutlet UIButton *button;

    CGPoint oldButtonCenter;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    oldButtonCenter = button.center;

}


- (IBAction)tapme:(id)sender {

    NSLog(@"tap! (the button's action method)");

}


- (IBAction)start:(id)sender {

    NSLog(@"you tapped Start");

    CGPoint goal = CGPointMake(100,400);

    button.center =oldButtonCenter;

      UIViewAnimationOptions opt =UIViewAnimationOptionAllowUserInteraction;

       [UIViewanimateWithDuration:10delay:0options:opt animations:^{

                button.center = goal;

      } completion:^(BOOL f) {

          }];

}


这样就可以在动画过程中实现点击事件了




0 0