• 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  

  • 相关阅读:
    关于SNMP的MIB文件的语法简述
    windows 与 Centos7 共享文件方法
    虚拟机 与 host主机,无法ping通的问题
    能ping通虚拟机,但snmp报文 Destination unreachable(Host administratively prohibited
    qt 打包发布 获取dll
    qt5.7.1 (create4.2.0)+msvc2015 安装后无法编译 & 缺少h文件
    windows 为qt5.7.1 安装openssl
    windows下为qt msvc版本配置调试器
    Rust中的类型大小总结
    substrate 为什么有的调用的地方用<T::Lookup as StaticLookup>::Source 代替 T::AccountId
  • 原文地址:https://www.cnblogs.com/xukunhenwuliao/p/3576209.html
Copyright © 2020-2023  润新知