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  

  • 相关阅读:
    管理博文 Web开发教程:Kendo UI for jQuery数据管理——列模板
    WPF界面开发新纪元——Diagram/Gantt控件升级
    Winform界面开发:WinForms应用程序中的MDI、SDI和MFI接口类型
    依赖注入[2]: 基于IoC的设计模式
    依赖注入[1]: 控制反转
    OAuth2.0的理解&基础
    开放平台鉴权以及OAuth2.0介绍
    OAuth2.0 知多少
    理解OAuth 2.0
    微服务架构介绍及开源框架
  • 原文地址:https://www.cnblogs.com/xukunhenwuliao/p/3576209.html
Copyright © 2011-2022 走看看