微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在可重用的 TableViewCell 中通过点击动画 UIImageView

如何解决在可重用的 TableViewCell 中通过点击动画 UIImageView

需要帮助:) 在下面的代码中 - 在最后渲染的可重用单元格中动画的图像,但我需要通过点击每个单独的动画。 “var imageView”也在顶部声明,如果在 cellForRowAt 中声明它 - 动画函数没有看到它:(

override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "userFriends",for: indexPath) as! TableViewCell
    
    let key = keys[indexPath.section]
    
    if searchBarStatus {
        let searchedFriends = filteredData[indexPath.row]
        cell.friendLabel.text = searchedFriends.fio
        imageName = UIImage(named: searchedFriends.userAvatar)!
    } else {
        let friendsClass = sections[key]![indexPath.row]
        cell.friendLabel.text = friendsClass.fio
        imageName = UIImage(named: friendsClass.userAvatar)!
    }
    
    let view = UIView(frame: CGRect(x: 3,y: 2,width: 42,height: 42))
    let imageSrc = imageName
    let image = imageSrc
    let imageView = UIImageView(image: image)
    imageView.frame = CGRect(x: 0,y: 0,height: 42)
    view.addSubview(imageView)
    view.clipsToBounds = true
    view.layer.masksToBounds = true
    view.layer.cornerRadius = view.frame.height / 2
    
    cell.addSubview(view)
    
    let tapGestureRecognizer = UITapGestureRecognizer(target: self,action: #selector(imageSpring(tapGestureRecognizer:)))
    imageView.isUserInteractionEnabled = true
    imageView.addGestureRecognizer(tapGestureRecognizer)
    
    return cell
}

动画代码

@objc func imageSpring(tapGestureRecognizer: UITapGestureRecognizer) {
    let pulse1 = CASpringAnimation(keyPath: "transform.scale")
    pulse1.duration = 0.6
    pulse1.fromValue = 1.0
    pulse1.tovalue = 1.12
    pulse1.autoreverses = true
    pulse1.repeatCount = 1
    pulse1.initialVeLocity = 0.5
    pulse1.damping = 0.8
    
    let animationGroup = CAAnimationGroup()
    animationGroup.duration = 2.7
    animationGroup.repeatCount = 1
    animationGroup.animations = [pulse1]
    imageView.layer.add(animationGroup,forKey: "pulse")
    print("Image Taped")
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。