在UITableViewCell中不可见的动画按钮

在UITableViewCell中不可见的动画按钮

问题描述:

我的应用程序有一个UITableView中的服务器列表,其中包含显示您是否使用映像连接到服务器的情况。目前这与静态图像正常工作。在UITableViewCell中不可见的动画按钮

我现在想添加一个非常基本的动画,显示它正在连接到服务器的过程中。只是在“连接”和“断开”图像之间闪烁。

但是,当我将按钮的图像设置为动画时,它根本不显示任何内容,而不是任何动画帧,而不是表格单元格的默认图像,只是空白。像这样创建一个动画在视图中不在表格中的按钮上工作,因此创建动画不是问题。它在按下按钮时开始播放,让我觉得按钮只需要刷新或强制重绘。但我一直无法找到任何东西。

我使用reloadData重新加载我的表。

(简体)ServerCell.h来源:

@interface ServerCell : UITableViewCell 

    @property (nonatomic, strong) IBOutlet UILabel *nameLabel; 
    @property (nonatomic, strong) IBOutlet UILabel *ipLabel; 
    @property (nonatomic, strong) IBOutlet UIImageView *serverImageView; 
    @property (weak, nonatomic) IBOutlet UIButton *mConnectionBtn; 

@end 

ServerCell.m

@implementation ServerCell 

#pragma mark - Properties 

@synthesize nameLabel; 
@synthesize ipLabel; 
@synthesize serverImageView; 
@synthesize mConnectionBtn; 

#pragma mark - Constructor/Destructor 

///////////////////////////////////////////////////////////////// 
/* 
* Class constructor 
*/ 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

#pragma mark - Class Methods 

///////////////////////////////////////////////////////////////// 
/* 
* Communicate to the superview the user selection 
*/ 
- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 
} 

@end 

的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Cast the TableCell to a custom ServerCell 
    ServerCell *cell = (ServerCell *)[tableView dequeueReusableCellWithIdentifier:@"ServerCell"]; 

    [cell.mConnectionBtn addTarget:self action:@selector(ActionServerCell:) forControlEvents:UIControlEventTouchUpInside]; 

    NSArray* images = [NSArray arrayWithObjects:[UIImage imageNamed:@"connected.png"], [UIImage imageNamed:@"disconnected.png"], nil]; 
    UIImage* anim = [UIImage animatedImageWithImages:images duration:1.0]; 

    [cell.mConnectionBtn setImage:anim forState:UIControlStateNormal]; 

    // return the cell object 
    return cell; 
} 

这应该做你想。当您调用开始动画时,您可能需要调整。

import UIKit 

class ExampleTableViewController: UITableViewController { 

    var names = ["John", "Will", "Ana"] 


    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return names.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cellIdentifier = "cell" 
     var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) 

     if cell == nil { 
      cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier) 
      cell?.imageView?.image = UIImage(named: "red")! 
      cell?.imageView?.animationImages = [UIImage(named: "red")!, UIImage(named: "green")!] 
      cell?.imageView?.animationDuration = 3 
      cell?.imageView?.animationRepeatCount = 0 
      cell?.imageView?.startAnimating() 
     } 
     cell!.textLabel?.text = names[indexPath.row] 
     return cell! 
    } 
} 
+0

我不得不对它进行修改以适应我的工作。但只是创建动画一次,然后运行startanimation或stopanimation取决于我需要解决它的概念。显然,将图像再次设置为同一个动画,原本是打破了一些东西,但仍然不知道为什么,但它现在起作用,这对我来说已经足够好了。非常感谢。 –

+0

没问题=)从我的测试中,我必须设置“imageView?.image”和“imageView?.animationImages”。只是设置“imageView?.animationImages”不起作用。很高兴它对你有效。 –

要显示的进程正在工作,你可以简单地添加一个activityIndicator而不是你的形象。

这可能比设置动画图像更简单。

  1. 将它连接到您厦门国际银行:

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

  1. 如果你不需要它只是在你设置你cell.m:
  2. cell.activityIndicator.hidden = YES;

    1. 然后你的进程正在开启:

    self.activityIndicator.hidden = NO;

    如果我错了,或者如果我missunderstood问题,请让我知道:)

开始=>