iPhone / Cocoa Touch - 将UITableViewCell背景从一种颜色更改为另一种颜色

时间:2013-04-26 21:50:20

标签: iphone ios objective-c cocoa-touch uigesturerecognizer

我想将UITableViewCell的颜色从一种颜色缓慢地改变为另一种颜色 WHILE 我持有(轻拍)单元格特定的持续时间。

在这段时间之后它应该做一个动作..就像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]];
    }];
}

**编辑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]];
            }];
        }
    }

3 个答案:

答案 0 :(得分: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]];
    }];
}

然后可能触摸这样的东西来关闭颜色 - 或者从细胞中做任何你想做的事。

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [UIView animateWithDuration:0.0 animations:^{
        [self setBackgroundColor:[UIColor whiteColor]];
    }];
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

UIView可以动态backgroundColor中的更改。每当选择一行时,将其设置为起始颜色,然后调用animateWithDuration:animations:以设置新颜色的动画。

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

编辑:检查this,了解如何正确设置UITableviewCell的背景色