ios - 将 Pan UIGesture 转换为滑动手势

标签 ios objective-c uigesturerecognizer uipangesturerecognizer uiswipegesturerecognizer

当用户将手指平移到特定单元格上时,单元格会根据用户的 x 轴改变颜色。我将如何从平移手势更改为滑动手势。我想构建它,而不是平移,在单元格上滑动会显示它背后的颜色。然后,当用户抬起手指时,电池应弹回原位。有小费吗?

  @synthesize _panRecognizer;

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
 {
if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])
{
    NSInteger emoteY = floor((self.frame.size.height - 32) / 2);

    _panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(respondToPanGesture:)];
    [_panRecognizer setDelegate:self];
    [_panRecognizer setMinimumNumberOfTouches:1];
    [_panRecognizer setMaximumNumberOfTouches:1];
    [self addGestureRecognizer:_panRecognizer];

 //        [[self textLabel] setFont:[UIFont boldSystemFontOfSize:18.0]];
    [[self textLabel] setFont:[UIFont systemFontOfSize:24.0]];
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];

    _border = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, self.frame.size.height)];
    [self addSubview:_border];

    _rightEmote = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width - 42, emoteY, 32, 32)];
    [self addSubview:_rightEmote];

    _leftEmote = [[UIImageView alloc] initWithFrame:CGRectMake(-32, emoteY, 32, 32)];
    [self addSubview:_leftEmote];
}

return self;
}

 - (void)setFrame:(CGRect)frame
 {
      [super setFrame:frame];

CGRect borderFrame = _border.frame,
rightEmoteFrame = _rightEmote.frame,
leftEmoteFrame = _leftEmote.frame;

NSInteger emoteY = floor((self.frame.size.height - 32) / 2);

borderFrame.size.height = self.frame.size.height;
rightEmoteFrame.origin.y = emoteY;
leftEmoteFrame.origin.y = emoteY;

[_border setFrame:borderFrame];
[_rightEmote setFrame:rightEmoteFrame];
[_leftEmote setFrame:leftEmoteFrame];
}

 - (DDFactor *)factor
{
    return _factor;
 }

 - (void)setFactor:(DDFactor *)factor
 {
_factor = factor;

- (void)respondToPanGesture:(UIGestureRecognizer *)recognizer
 {
 //    NSLog(@"%lu", [recognizer state]);

NSInteger rstate = [recognizer state];
CGFloat touchX = 0.0;

if ([recognizer numberOfTouches] == 1)
{
    touchX = [recognizer locationOfTouch:0 inView:self].x;

    if (rstate == UIGestureRecognizerStateBegan)
    {
        /*
         animate to color under touch
         */

        CGRect labelFrame = [self textLabel].frame;
        labelFrame.origin.x += 42;

        CGRect rightEmoteFrame = _rightEmote.frame;
        rightEmoteFrame.origin.x += 42;
        CGRect leftEmoteFrame = _leftEmote.frame;
        leftEmoteFrame.origin.x += 42;

        [UIView animateWithDuration:0.3 animations:^{
            [_border setAlpha:0.0];
            [[self textLabel] setTextColor:[UIColor whiteColor]];
            [[self textLabel] setFrame:labelFrame];
            [_rightEmote setFrame:rightEmoteFrame];
            [_leftEmote setFrame:leftEmoteFrame];
        }];
        [self animateEmoticonsWithColor:NO duration:0.3];
    }
    else if (rstate == UIGestureRecognizerStateChanged)
    {
        /*
         alter color
         trigger emote animation if necessary
         */

        if ([self responseForTouchPosition:touchX] != _responseValue)
        {
            [self setResponseValue:[self responseForTouchPosition:touchX]];
            [_border setBackgroundColor:[UIColor colorForResponse:_responseValue]];
            [self animateToBackgroundColor:[UIColor colorForResponse:_responseValue]];
            [self animateEmoticonsWithColor:NO duration:0.2];
        }
    }
}
else if (([recognizer numberOfTouches] == 0) && (rstate == 3))
{
    CGRect labelFrame = [self textLabel].frame;
    labelFrame.origin.x -= 42;

    CGRect rightEmoteFrame = _rightEmote.frame;
    rightEmoteFrame.origin.x -= 42;
    CGRect leftEmoteFrame = _leftEmote.frame;
    leftEmoteFrame.origin.x -= 42;

    [UIView animateWithDuration:0.3 animations:^{
        [_border setAlpha:1.0];
        [_rightEmote setFrame:rightEmoteFrame];
        [_leftEmote setFrame:leftEmoteFrame];
        [[self textLabel] setFrame:labelFrame];
        [[self textLabel] setTextColor:[UIColor colorForResponse:_responseValue]];
        [self setBackgroundColor:[[UIColor colorForResponse:_responseValue] colorWithAlphaComponent:0.1]];
    }];
    [self animateEmoticonsWithColor:YES duration:0.3];
}

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

// Configure the view for the selected state
 }

- (DDResponseValue)responseForTouchPosition:(CGFloat)x
 {
DDResponseValue response;

if (x <= 70) response = DDResponseGrin;
else if (x <= 120) response = DDResponseSmile;
else if (x <= 170) response = DDResponseSad;
else if (x <= 220) response = DDResponseNervous;
else if (x <= 270) response = DDResponseRefusal;
else response = DDResponseNeutral;

return response;
 }

最佳答案

您可以使用 UISwipeGestureRecognizer 状态并可以按照您的意愿进行操作。看看下面的代码,

- (void)swipeGestureMethod:(UISwipeGestureRecognizer *)recognizer {

   CGPoint point = [recognizer locationInView:[recognizer view]];
   if (recognizer.state == UIGestureRecognizerStateBegan) {

   } else if (recognizer.state == UIGestureRecognizerStateEnded) {

   }

}

关于ios - 将 Pan UIGesture 转换为滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19698926/

相关文章:

ios - 长按时单元格 ImageView 更改

ios - CGImageCreate 测试模式不工作 (iOS)

objective-c - 两个 UISplitViewController,1 个(共享)详细 View

c++ - OpenSSL CMS 在 C++ 和 Objective-c 中加密

ios - Swift 错误,UIMenuItem 到 UIview/UIlabel 进行复制

ios - UIStackView 和手势

ios - 无法启用解析本地数据存储

iphone - 解密md5 iphone

ios - GCD dispatch_async 内部的 NativeX fetchAd 调用。 Phonegap插件开发

ios - 如何将 `accessibilityLabel` 添加到 `UIAlertView` 按钮?