0

我想将UITableViewCell的颜色从一种颜色缓慢更改为另一种颜色,同时我将单元格保持(点击保持)特定持续时间。

在那段时间之后,它应该执行一个动作。它就像 Android 上的 Whatsapps 行选择一样。它首先是亮的,最后是最暗的颜色。

我已经用手势识别器试过了,但是没有用..

那可能吗?我会很感激一个示例代码。

编辑1:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    CostumEventCell *cell = [eventsTable cellForRowAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor colorWithRed:(252/255.0) green:(213/255.0) blue:(183/255.0) alpha:1]];

    [UIView animateWithDuration:3.0 animations:^(void) {
        [cell setBackgroundColor:[UIColor colorWithRed:(204/255.0) green:(85/255.0) blue:(0/255.0) alpha:1]];
    }];
}

** EDIT 2(带有手势识别器):**

- (void) createGestureListener{
    UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)];
    longPressRecognizer.delegate = self;
    [self.eventsTable addGestureRecognizer:longPressRecognizer];


}

    -(void)onLongPress:(UILongPressGestureRecognizer*)pGesture
    {

        CGPoint touchPoint = [pGesture locationInView:self.view];
        NSIndexPath* row = [eventsTable indexPathForRowAtPoint:touchPoint];

        if (pGesture.state == UIGestureRecognizerStateBegan)
        {
            //Do something to tell the user!
            NSLog(@"Tap started");
            CostumEventCell *cell = [eventsTable cellForRowAtIndexPath:row];

            [UIView animateWithDuration:3.0 animations:^(void) {
                [cell setBackgroundColor:[UIColor colorWithRed:(252/255.0) green:(213/255.0) blue:(183/255.0) alpha:1]];
                [cell setBackgroundColor:[UIColor colorWithRed:(204/255.0) green:(85/255.0) blue:(0/255.0) alpha:1]];
            }];
        }
    }
4

3 回答 3

1

我会将自己的单元格类作为 UITableViewCell 的子类并拦截其中的触摸。

覆盖 touchesBegan 像这样

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    [UIView animateWithDuration:3.0 animations:^(void) {
        [self setBackgroundColor:[UIColor colorWithRed:(252/255.0) green:(213/255.0) blue:(183/255.0) alpha:1]];
        [self setBackgroundColor:[UIColor colorWithRed:(204/255.0) green:(85/255.0) blue:(0/255.0) alpha:1]];
    }];
}

然后可能会像这样 touchesEnded 来关闭颜色——或者从单元格中做任何你想做的事情。

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [UIView animateWithDuration:0.0 animations:^{
        [self setBackgroundColor:[UIColor whiteColor]];
    }];
}
于 2013-04-27T21:04:12.827 回答
0

您可以在 Cell 上添加一个 UIButton,然后通过添加外观动画来处理 touch down 事件。

于 2013-04-27T02:03:32.753 回答
0

UIView可以动画变化backgroundColor。Whenever a row is selected, set it to the starting color and then call animateWithDuration:animations:to animate to the new color.

//set first color here
[UIView animateWithDuration:1.0 animations:^{
     // set final color here.
}];

编辑:检查这个以了解如何正确设置背景颜色UITableviewCell

于 2013-04-27T03:54:51.237 回答