zoukankan      html  css  js  c++  java
  • button移动(或执行动画)之后无法响应点击事件的解决方法

    其实问题的本质原因是button在移动之后一直处于按下的状态.导致无法触发方法.我们要做的就是在移动后取消button的按下状态.以下为MyButton的.m文件.我的自定义button模仿了苹果的虚拟home键.拖拽后有自动向左或者向右(根据坐标)的效果.

    @interface MyButton : UIButton
    {
        CGPoint beginPoint;
    }
    @property(nonatomic) BOOL dragEnable;
    @end
    以下为.m文件
    #import "MyButton.h"
    
    @implementation MyButton
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor redColor];
        }
        return self;
    }
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesBegan:touches withEvent:event];
        if (!_dragEnable) {
            return;
        }
        UITouch *touch = [touches anyObject];
        beginPoint = [touch locationInView:self];
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if (!_dragEnable) {
            return;
        }
        UITouch *touch = [touches anyObject];
        CGPoint nowPoint = [touch locationInView:self];
        float offsetX = nowPoint.x - beginPoint.x;
        float offsetY = nowPoint.y - beginPoint.y;
        self.center = CGPointMake(self.center.x + offsetX, self.center.y +offsetY);
        //CGPoint next = [touch previousLocationInView:self];
    }
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if (!_dragEnable) {
            return;
        }
        self.center = CGPointMake(self.center.x, self.center.y);
        if (self.center.x<(320-self.center.x)) {
            CAKeyframeAnimation *frameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
            frameAnimation.duration = 1*(self.center.x)/160;
            frameAnimation.values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:self.center],[NSValue valueWithCGPoint:CGPointMake(self.frame.size.width/2, self.center.y)], nil];
            
            [self.layer addAnimation:frameAnimation forKey:@"sss"];
            self.center = CGPointMake(self.frame.size.width/2, self.center.y);
        }
        else{
            CAKeyframeAnimation *frameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
            frameAnimation.duration = 1*(320 - self.center.x)/160;
            frameAnimation.values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:self.center],[NSValue valueWithCGPoint:CGPointMake(320 - self.frame.size.width/2, self.center.y)], nil];
            
            [self.layer addAnimation:frameAnimation forKey:@"sss"];
            
           self.center = CGPointMake(320 - self.frame.size.width/2, self.center.y);
        }
        NSLog(@"%@",NSStringFromCGRect(self.frame));
        [super touchesEnded: touches withEvent: event];
    }
    
    问题的关键是

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event中的  [super touchesBegan:touches withEvent:event];

     - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event   中的 [super touchesEnded: touches withEvent: event];

    加入这两个方法后,button就可以取消按下状态了.


    转载请注明本文源地址   blog.csdn.net/u013082522/article/details/183172uibutton91  

  • 相关阅读:
    Ubuntu上安装RabbitMQ
    解决tfs工作区绑定问题
    ubuntu装个nginx
    supervisor执行dotnet
    Validate + Boostrap tooltip 表单验证示例
    ASP.NET下跨应用共享Session和使用Redis进行Session托管
    IE下将网页拷贝到剪贴板
    js格式化json格式的日期
    js 时间戳转换
    Windows服务简单使用
  • 原文地址:https://www.cnblogs.com/xukunhenwuliao/p/3576209.html
Copyright © 2011-2022 走看看