检测customcell上的按钮单击事件

Posted

技术标签:

【中文标题】检测customcell上的按钮单击事件【英文标题】:Detect button click event on customcell 【发布时间】:2017-08-02 04:56:29 【问题描述】:

我添加了一个自定义单元格,如下所示用于步进进度。 UI 透视图,它看起来是我想要的,但我无法弄清楚如何确定按钮是否已被点击。

我的灵感来自https://github.com/yenbekbay/AYStepperView,但是这个有 PageViewController,我无法将它添加到 tableCell 中。

#import "StepperProgressTableViewCell.h"
#import "AYStepperView.h"

static CGFloat const kFormStepperViewHeight = 80;

@interface StepperProgressTableViewCell ()

@property (nonatomic) AYStepperView *stepperView;
@property (nonatomic) NSUInteger currentIndex;
@property (nonatomic) NSUInteger currentStep;

@end

@implementation StepperProgressTableViewCell

- (void)awakeFromNib 
    [super awakeFromNib];
    // Initialization code
    [self setUpViews];
    self.currentIndex = 0;
    self.currentStep = 0;


- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
    [super setSelected:selected animated:animated];


#pragma mark Private

- (void)setUpViews 

    self.stepperView = [[AYStepperView alloc]initWithFrame:CGRectMake(0, 40 , self.frame.size.width, kFormStepperViewHeight) 
                         titles:@[NSLocalizedString(@"Start", nil), 
                                 NSLocalizedString(@"Cooking", nil), 
                                 NSLocalizedString(@"Ready", nil)]];
    self.stepperView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    self.stepperView.userInteractionEnabled = YES;
    [self addSubview:self.stepperView];

    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.stepperView.frame), CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) - CGRectGetMaxY(self.stepperView.frame))];
    [self addSubview:self.containerView];


@end

AYStepperView.m

#import "AYStepperView.h"

#import <pop/POP.h>

static UIEdgeInsets const kStepperViewPadding = 
  15, 0, 15, 0
;
static CGFloat const kStepperLabelsSpacing = 10;
static CGFloat const kStepperPipeHeight = 5;

@interface AYStepperView ()

@property (nonatomic) UIView *pipeView;
@property (nonatomic) UIView *labelsView;
@property (nonatomic) UIView *pipeBackgroundView;
@property (nonatomic) UIView *pipeFillView;
@property (nonatomic) NSMutableArray *stepLabels;

@end

@implementation AYStepperView

#pragma mark Initialization

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles 
  self = [super initWithFrame:frame];
  if (!self) 
    return nil;
  

  _titles = titles;
  self.backgroundColor = [UIColor colorWithRed:0.98f green:0.98f blue:0.98f alpha:1];
  self.tintColor = [UIColor colorWithRed:0.2f green:0.29f blue:0.37f alpha:1];

  self.pipeView = [[UIView alloc] initWithFrame:CGRectMake(kStepperViewPadding.left, kStepperViewPadding.top, CGRectGetWidth(self.bounds) - kStepperViewPadding.left - kStepperViewPadding.right, CGRectGetHeight(self.bounds) / 2 - kStepperViewPadding.top)];
  [self addSubview:self.pipeView];

  self.labelsView = [[UIView alloc] initWithFrame:CGRectMake(kStepperViewPadding.left, CGRectGetMaxY(self.pipeView.frame) + kStepperViewPadding.top, CGRectGetWidth(self.bounds) - kStepperViewPadding.left - kStepperViewPadding.right, CGRectGetHeight(self.bounds) / 2 - kStepperViewPadding.top - kStepperViewPadding.bottom)];
  [self addSubview:self.labelsView];

  self.pipeBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, (CGRectGetHeight(self.pipeView.bounds) - kStepperPipeHeight) / 2, CGRectGetWidth(self.pipeView.bounds), kStepperPipeHeight)];
  self.pipeBackgroundView.backgroundColor = [UIColor lightGrayColor];
  [self.pipeView addSubview:self.pipeBackgroundView];

  CGRect pipeFillViewFrame = self.pipeBackgroundView.frame;
  pipeFillViewFrame.size.width = 0;
  self.pipeFillView = [[UIView alloc] initWithFrame:pipeFillViewFrame];
  self.pipeFillView.backgroundColor = self.tintColor;
  [self.pipeView addSubview:self.pipeFillView];

  _stepButtons = [NSMutableArray new];
  _stepLabels = [NSMutableArray new];
  for (NSUInteger i = 0; i < titles.count; i++) 
    UIButton *stepButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, CGRectGetHeight(self.pipeView.bounds), CGRectGetHeight(self.pipeView.bounds))];
    stepButton.center = CGPointMake(CGRectGetWidth(self.pipeView.bounds) * (i + 0.5f) / titles.count, stepButton.center.y);
    stepButton.clipsToBounds = YES;
    stepButton.tag = i;
    stepButton.layer.cornerRadius = CGRectGetHeight(stepButton.bounds) / 2;
    stepButton.backgroundColor = [UIColor lightGrayColor];
    [self.pipeView addSubview:stepButton];
    [self.stepButtons addObject:stepButton];

    UILabel *stepLabel = [UILabel new];
    stepLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
    stepLabel.textColor = self.tintColor;
    stepLabel.textAlignment = NSTextAlignmentCenter;
    stepLabel.text = titles[i];
    stepLabel.numberOfLines = 0;
    stepLabel.frame = (CGRect) 
      stepLabel.frame.origin, [stepLabel sizeThatFits:CGSizeMake(CGRectGetWidth(self.pipeView.bounds) / titles.count - kStepperLabelsSpacing, 0)]
    ;
    stepLabel.center = CGPointMake(CGRectGetWidth(self.labelsView.bounds) * (i + 0.5f) / titles.count, CGRectGetHeight(self.labelsView.bounds) / 2);
    [self.labelsView addSubview:stepLabel];
    [self.stepLabels addObject:stepLabel];
  
  _currentStepIndex = 0;
  [self completeStepAtIndex:0 until:1 completionBlock:nil];

  return self;


#pragma mark Public

- (void)updateCurrentStepIndex:(NSUInteger)currentStepIndex completionBlock:(void (^)())completionBlock 
  if (currentStepIndex >= self.titles.count || currentStepIndex == self.currentStepIndex) 
    if (completionBlock) 
      completionBlock();
    
   else 
    NSUInteger previousStepIndex = self.currentStepIndex;
    _currentStepIndex = currentStepIndex;
    if ((NSInteger)currentStepIndex - (NSInteger)previousStepIndex > 0) 
      [self completeStepAtIndex:previousStepIndex + 1 until:currentStepIndex + 1 completionBlock:completionBlock];
     else 
      [self uncompleteStepAtIndex:previousStepIndex until:currentStepIndex - 1 completionBlock:completionBlock];
    
  


#pragma mark Setters

- (void)setTintColor:(UIColor *)tintColor 
  _tintColor = tintColor;
  self.pipeFillView.backgroundColor = tintColor;
  for (UILabel *label in self.stepLabels) 
    label.textColor = tintColor;
  
  [self.stepButtons[self.currentStepIndex] setBackgroundColor:tintColor];


#pragma mark Private

- (void)completeStepAtIndex:(NSUInteger)index until:(NSUInteger)until completionBlock:(void (^)())completionBlock 
  if (index == until) 
    if (completionBlock) 
      completionBlock();
    
   else 
    [UIView animateWithDuration:0.2f animations:^
      CGRect pipeFillViewFrame = self.pipeFillView.frame;
       NSLog(@"%lu, %lu",until, index);
        if(index == _titles.count - 1)
        
            pipeFillViewFrame.size.width = CGRectGetWidth(self.pipeBackgroundView.bounds) * (index + 1.0f) / self.titles.count;
        
        else
        
            pipeFillViewFrame.size.width = CGRectGetWidth(self.pipeBackgroundView.bounds) * (index + 0.5f) / self.titles.count;
        
      self.pipeFillView.frame = pipeFillViewFrame;
     completion:^(BOOL finishedWidthAnimation) 
      [self completeStepAtIndex:index + 1 until:until completionBlock:completionBlock];
      UIView *stepButton = self.stepButtons[index];
      stepButton.backgroundColor = self.tintColor;
      POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
      scaleAnimation.velocity = [NSValue valueWithCGSize:CGSizeMake(3.f, 3.f)];
      scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
      scaleAnimation.springBounciness = 5.f;
      [stepButton.layer pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
    ];
  


- (void)uncompleteStepAtIndex:(NSUInteger)index until:(NSUInteger)until completionBlock:(void (^)())completionBlock 
  if (index == until) 
    if (completionBlock) 
      completionBlock();
    
   else 
    if (index > until + 1) 
      UIView *stepButton = self.stepButtons[index];
      stepButton.backgroundColor = [UIColor lightGrayColor];
      POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
      scaleAnimation.velocity = [NSValue valueWithCGSize:CGSizeMake(3.f, 3.f)];
      scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
      scaleAnimation.springBounciness = 5.f;
      [stepButton.layer pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
    
    [UIView animateWithDuration:0.2f animations:^
      CGRect pipeFillViewFrame = self.pipeFillView.frame;
      pipeFillViewFrame.size.width = CGRectGetWidth(self.pipeBackgroundView.bounds) * (index + 0.5f) / self.titles.count;
      self.pipeFillView.frame = pipeFillViewFrame;
     completion:^(BOOL finishedWidthAnimation) 
      [self uncompleteStepAtIndex:index - 1 until:until completionBlock:completionBlock];
    ];
  


@end

【问题讨论】:

请不要发布您的整个代码,而只发布与您的问题相关的部分。 【参考方案1】:

如果您想从单元格捕获事件到视图控制器,最简单的方法是创建一个协议并将视图控制器设置为单元格的委托。我相信这里有很多类似的问题可以帮助你喜欢this one。

【讨论】:

以上是关于检测customcell上的按钮单击事件的主要内容,如果未能解决你的问题,请参考以下文章

无法检测到按钮上的“滑动打开”事件

单击事件上的 Kentico 自定义 WebPart 按钮

如何在 Java 中检测 Image 对象上的鼠标单击事件?

显示软键盘时,Android 设备上的按钮单击事件未触发。

用户单击 ASP.NET Web 表单上的按钮与计时器运行调用相同按钮单击事件的方法之间的区别?

离子按钮单击上的事件 preventDefault 不起作用