Ios 实施时的异常行为;例如;及;“没有”;打火牌角落的动画

Ios 实施时的异常行为;例如;及;“没有”;打火牌角落的动画,ios,swift,uikit,swipe,Ios,Swift,Uikit,Swipe,我正在尝试实现“喜欢”和“不喜欢”的动画,当你在Tinder上刷卡时,它们会出现在卡片/个人资料的角落 然而,有一种奇怪的行为,我不明白为什么会发生 当我第一次刷个人资料时,“喜欢”(勾号)或“不喜欢”(十字)会出现在角落里,就像我想要的那样;但是,如果我让卡回到原来的位置,而没有跳过或喜欢配置文件,然后我再次刷卡,卡的中间会出现“喜欢”(勾号)或“不喜欢”(十字) 当我第一次刷卡时: 当我稍微刷卡时,松开它,让它回到原来的位置,然后再次刷卡: 我不明白为什么会发生这种情况,因为每次我刷卡时

我正在尝试实现“喜欢”和“不喜欢”的动画,当你在Tinder上刷卡时,它们会出现在卡片/个人资料的角落

然而,有一种奇怪的行为,我不明白为什么会发生

当我第一次刷个人资料时,“喜欢”(勾号)或“不喜欢”(十字)会出现在角落里,就像我想要的那样;但是,如果我让卡回到原来的位置,而没有跳过或喜欢配置文件,然后我再次刷卡,卡的中间会出现“喜欢”(勾号)或“不喜欢”(十字)

当我第一次刷卡时:

当我稍微刷卡时,松开它,让它回到原来的位置,然后再次刷卡:

我不明白为什么会发生这种情况,因为每次我刷卡时,会触发渲染动画以正确显示的部分(在卡的拐角处)。我通过设置断点来检查它

我不知道这是怎么发生的。请告诉我该如何修理它

这是我的SwipeCardView的代码

class SwipeCardView : UIView {
    
    //MARK: - Properties
    var swipeView : UIView!
    var shadowView : UIView!
    var matchImageView: UIImageView!
    
    var isLikeImageView: UIImageView!
    
    var label = UILabel()
    var moreButton = UIButton()
    
    var delegate : SwipeCardsDelegate?
    
    var divisor : CGFloat = 0
    let baseView = UIView()
    
    var dataSource : CardsDataModel? {
        didSet {
            swipeView.backgroundColor = dataSource?.bgColor
            label.text = dataSource?.text
            guard let image = dataSource?.image else { return }
            matchImageView.image = UIImage(named: image)
        }
    }
    
    
    //MARK: - Init
    override init(frame: CGRect) {
        super.init(frame: .zero)
        configureShadowView()
        configureSwipeView()
        configureLabelView()
        configureImageView()
        configureButton()
        addPanGestureOnCards()
        configureTapGesture()
        configureIsLike()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - Configuration
    
    func configureShadowView() {
        shadowView = UIView()
        shadowView.backgroundColor = .clear
        shadowView.layer.shadowColor = UIColor.black.cgColor
        shadowView.layer.shadowOffset = CGSize(width: 0, height: 0)
        shadowView.layer.shadowOpacity = 0.8
        shadowView.layer.shadowRadius = 4.0
        addSubview(shadowView)
        
        shadowView.translatesAutoresizingMaskIntoConstraints = false
        shadowView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        shadowView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
        shadowView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        shadowView.topAnchor.constraint(equalTo: topAnchor).isActive = true
    }
    
    func configureSwipeView() {
        swipeView = UIView()
        swipeView.layer.cornerRadius = 15
        swipeView.clipsToBounds = true
        shadowView.addSubview(swipeView)
        
        swipeView.translatesAutoresizingMaskIntoConstraints = false
        swipeView.leftAnchor.constraint(equalTo: shadowView.leftAnchor).isActive = true
        swipeView.rightAnchor.constraint(equalTo: shadowView.rightAnchor).isActive = true
        swipeView.bottomAnchor.constraint(equalTo: shadowView.bottomAnchor).isActive = true
        swipeView.topAnchor.constraint(equalTo: shadowView.topAnchor).isActive = true
    }
    
    func configureLabelView() {
        swipeView.addSubview(label)
        label.backgroundColor = .white
        label.textColor = .black
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 18)
        label.translatesAutoresizingMaskIntoConstraints = false
        label.leftAnchor.constraint(equalTo: swipeView.leftAnchor).isActive = true
        label.rightAnchor.constraint(equalTo: swipeView.rightAnchor).isActive = true
        label.bottomAnchor.constraint(equalTo: swipeView.bottomAnchor).isActive = true
        label.heightAnchor.constraint(equalToConstant: 85).isActive = true
        
    }
    
    func configureImageView() {
        matchImageView = UIImageView()
        swipeView.addSubview(matchImageView)
        matchImageView.contentMode = .scaleAspectFill
        matchImageView.translatesAutoresizingMaskIntoConstraints = false
        
        matchImageView.centerXAnchor.constraint(equalTo: swipeView.centerXAnchor).isActive = true
        matchImageView.centerYAnchor.constraint(equalTo: swipeView.centerYAnchor, constant: -30).isActive = true
        matchImageView.topAnchor.constraint(equalTo: swipeView.topAnchor).isActive = true
        //        imageView.bottomAnchor.constraint(equalTo: swipeView.bottomAnchor, constant: -85).isActive = true
        
    }
    
    func configureButton() {
        label.addSubview(moreButton)
        moreButton.translatesAutoresizingMaskIntoConstraints = false
        let image = UIImage(named: "plus-tab")?.withRenderingMode(.alwaysTemplate)
        moreButton.setImage(image, for: .normal)
        moreButton.tintColor = UIColor.red
        
        moreButton.rightAnchor.constraint(equalTo: label.rightAnchor, constant: -15).isActive = true
        moreButton.centerYAnchor.constraint(equalTo: label.centerYAnchor).isActive = true
        moreButton.widthAnchor.constraint(equalToConstant: 50).isActive = true
        moreButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
    }
    
    func configureIsLike() {
        isLikeImageView = UIImageView()
        swipeView.addSubview(isLikeImageView)
        isLikeImageView.image = UIImage(named: "like")
        isLikeImageView.alpha = 0
        isLikeImageView.contentMode = .scaleAspectFit
        isLikeImageView.translatesAutoresizingMaskIntoConstraints = false
        isLikeImageView.topAnchor.constraint(equalTo: swipeView.topAnchor).isActive = true
        isLikeImageView.widthAnchor.constraint(equalToConstant: 30).isActive = true
        isLikeImageView.heightAnchor.constraint(equalToConstant: 50).isActive = true

//        isLikeImageView.frame = CGRect(x: self.swipeView.width / 2, y: self.frame.height / 2, width: 100, height: 100)
    }
    
    func configureTapGesture() {
        addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapGesture)))
    }
    
    
    func addPanGestureOnCards() {
        self.isUserInteractionEnabled = true
        addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture)))
    }
        
    //MARK: - Handlers
    @objc func handlePanGesture(sender: UIPanGestureRecognizer){
        let card = sender.view as! SwipeCardView
        let point = sender.translation(in: self)
        let centerOfParentContainer = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
        card.center = CGPoint(x: centerOfParentContainer.x + point.x, y: centerOfParentContainer.y + point.y)
        
        let xFromCenter = card.center.x - centerOfParentContainer.x
        print(xFromCenter)
        let distanceFromCenter = ((UIScreen.main.bounds.width / 2) - card.center.x)
        divisor = ((UIScreen.main.bounds.width / 2) / 0.61)
        
        print(card.center.x)
        

// The part that is supposed to trigger the animations correctly
        if xFromCenter > 0 {
            isLikeImageView.image = UIImage(named: "like")
            isLikeImageView.tintColor = UIColor.green
            isLikeImageView.leadingAnchor.constraint(equalTo: self.swipeView.leadingAnchor).isActive = true
            isLikeImageView.widthAnchor.constraint(equalToConstant: 50).isActive = true
        } else {
            isLikeImageView.image = UIImage(named: "skip")
            isLikeImageView.tintColor = UIColor.red
            isLikeImageView.trailingAnchor.constraint(equalTo: self.swipeView.trailingAnchor).isActive = true
            isLikeImageView.widthAnchor.constraint(equalToConstant: 50).isActive = true

        }
        
        isLikeImageView.alpha = abs(xFromCenter) / centerOfParentContainer.x
        
        switch sender.state {
        case .ended:
            if card.center.x > (self.frame.width - 75) {
                delegate?.swipeDidEnd(on: card)
                UIView.animate(withDuration: 0.3) {
                    card.center = CGPoint(x: centerOfParentContainer.x + point.x + 200, y: centerOfParentContainer.y + point.y + 75)
                    card.alpha = 0
                    self.layoutIfNeeded()
                }
                return
            }else if card.center.x < 75  {
                delegate?.swipeDidEnd(on: card)
                UIView.animate(withDuration: 0.3) {
                    card.center = CGPoint(x: centerOfParentContainer.x + point.x - 200, y: centerOfParentContainer.y + point.y + 75)
                    card.alpha = 0
                    self.layoutIfNeeded()
                }
                return
            }
            UIView.animate(withDuration: 0.2) {
                card.transform = .identity
                card.center = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
                self.isLikeImageView.alpha = 0
                self.layoutIfNeeded()
            }
            
        case .changed:
            let rotation = tan(point.x / (self.frame.width * 2.0))
            card.transform = CGAffineTransform(rotationAngle: rotation)
            
        default:
            break
        }
    }
    
    @objc func handleTapGesture(sender: UITapGestureRecognizer){
    }
}

protocol SwipeCardsDataSource {
    func numberOfCardsToShow() -> Int
    func card(at index: Int) -> SwipeCardView
    func emptyView() -> UIView?
    
}

protocol SwipeCardsDelegate {
    func swipeDidEnd(on view: SwipeCardView)
}

class SwipeCardView:UIView{
//标记:-属性
var swipeView:UIView!
var shadowView:UIView!
var matchImageView:UIImageView!
var isLikeImageView:UIImageView!
var label=UILabel()
var moreButton=UIButton()
var代表:瑞士银行?
变量除数:CGFloat=0
让baseView=UIView()
var数据源:CardsDataModel{
迪塞特{
swipeView.backgroundColor=数据源?.bgColor
label.text=数据源?.text
guard let image=dataSource?.image else{return}
matchImageView.image=UIImage(名称:image)
}
}
//MARK:-Init
重写初始化(帧:CGRect){
super.init(帧:.0)
配置ShadowView()
配置SwipeView()
配置LabelView()
configureImageView()
配置按钮()
AddPangEstureCards()
configureTap手势()
configureIsLike()
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
//标记:-配置
func configureShadowView(){
shadowView=UIView()
shadowView.backgroundColor=.clear
shadowView.layer.shadowColor=UIColor.black.cgColor
shadowView.layer.shadowOffset=CGSize(宽度:0,高度:0)
shadowView.layer.shadowOpacity=0.8
shadowView.layer.shadowRadius=4.0
添加子视图(阴影视图)
shadowView.translatesAutoresizingMaskIntoConstraints=false
shadowView.leftAnchor.constraint(equalTo:leftAnchor).isActive=true
shadowView.rightAnchor.constraint(equalTo:rightAnchor).isActive=true
shadowView.bottomAnchor.constraint(equalTo:bottomAnchor).isActive=true
shadowView.topAnchor.constraint(equalTo:topAnchor).isActive=true
}
func配置SwipeView(){
swipeView=UIView()
swipeView.layer.cornerRadius=15
swipeView.clipstobunds=true
shadowView.addSubview(swipeView)
swipeView.translatesAutoResizezingMaskintoConstraints=false
swipeView.leftAnchor.constraint(equalTo:shadowView.leftAnchor).isActive=true
swipeView.rightAnchor.constraint(equalTo:shadowView.rightAnchor).isActive=true
swipeView.bottomAnchor.constraint(equalTo:shadowView.bottomAnchor).isActive=true
swipeView.topAnchor.constraint(equalTo:shadowView.topAnchor).isActive=true
}
函数配置LabelView(){
swipeView.addSubview(标签)
label.backgroundColor=.white
label.textColor=.black
label.textAlignment=.center
label.font=UIFont.systemFont(尺寸:18)
label.translatesAutoresizingMaskIntoConstraints=false
label.leftAnchor.constraint(equalTo:swipeView.leftAnchor).isActive=true
label.rightanch.constraint(equalTo:swipeView.rightanch).isActive=true
label.bottomAnchor.constraint(equalTo:swipeView.bottomAnchor).isActive=true
label.heightAnchor.constraint(equalToConstant:85).isActive=true
}
func configureImageView(){
matchImageView=UIImageView()
swipeView.addSubview(匹配图像视图)
matchImageView.contentMode=.ScaleSpectFill
matchImageView.TranslatesAutoResizezingMaskintoConstraints=false
matchImageView.centerXAnchor.constraint(等同于:swipeView.centerXAnchor).isActive=true
matchImageView.centerYAnchor.constraint(等式:swipeView.centerYAnchor,常数:-30)。isActive=true
matchImageView.topAnchor.constraint(equalTo:swipeView.topAnchor).isActive=true
//imageView.bottomAnchor.constraint(等式:swipeView.bottomAnchor,常数:-85)。isActive=true
}
函数配置按钮(){
label.addSubview(更多按钮)
moreButton.translatesAutoresizingMaskIntoConstraints=false
让image=UIImage(名为:“plus tab”)?。使用渲染模式(.alwaysTemplate)
moreButton.setImage(图像,用于:。正常)
moreButton.tintColor=UIColor.red
moreButton.rightAnchor.constraint(等式:label.rightAnchor,常量:-15)。isActive=true
moreButton.centerYAnchor.constraint(equalTo:label.centerYAnchor).isActive=true
moreButton.widthAnchor.constraint(equalToConstant:50).isActive=true
moreButton.heightAnchor.constraint(equalToConstant:50).isActive=true
}
func configureIsLike(){
isLikeImageView=UIImageView()
swipeView.addSubview(isLikeImageView)
isLikeImageView.image=UIImage(名为“like”)
isLikeImageView.alpha=0
伊斯利凯马