2017-08-02 86 views
0

我已經爲步進器進度添加了如下定製單元。用戶界面的角度來看,它看起來是我想要的,但我無法弄清楚我如何能夠確定是否點擊了按鈕。定製單元上的檢測按鈕單擊事件

我的靈感來自於https://github.com/yenbekbay/AYStepperView,但這個有PageViewController,我無法將它添加到tableCell中。

enter image description here

#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 
+0

請不要發佈您的整個代碼,但只有與您的問題相關的部分。 – phi

回答

0

如果你想趕上從該視圖控制器單元格的事件,最簡單的方法是創建一個協議,並設置視圖控制器作爲單元的代表。我相信在這裏有很多類似的問題可以幫助你,例如this one