动画时的UserInteraction

时间:2018-09-05 18:34:37

标签: swift animation

我正在以圆形方式围绕视图旋转几个圆形按钮。在旋转时,如果用户单击任何按钮,则不会发生任何事件。 使用下面的代码。 请参见addButton函数的代码。这就是我添加按钮的方式。然后创建TrieView对象并将其添加到目标视图中,然后调用addAnimation方法。

import Foundation

class TribeView: UIView {
 var circleTapBlock:((Int) -> Void)?
 var nameLabel:UILabel!
 var type = CircleType.TribeHomeCenter
 var index = 99999999999

 private var bgImageView = UIImageView()
 private var whiteCircle = UIView()
 private var profilePic = AvatarImageView()
 private var titleLabel = UILabel()

enum CircleType {
    case TribeHomeCenter
    case TribeHomeOrbit
}

var isFreeSize:Bool {
    switch type {
    case .TribeHomeCenter,.TribeHomeOrbit:
        return false
    }
}

var bgImageTopConstant:CGFloat{
    switch type {
    case .TribeHomeCenter:
        return -22
    case .TribeHomeOrbit:
        return -22
    }
}

static func viewSize(for type:CircleType) -> CGSize {
    switch type {
    case .TribeHomeCenter:
        return CGSize(width: 80, height: 100)
    case .TribeHomeOrbit:
        return CGSize(width: 60, height: 80)
    }
}


var bgImagesize:CGSize{
    switch type {
    case .TribeHomeCenter:
        return CGSize(width: 135, height: 135)
    case .TribeHomeOrbit:
        return CGSize(width: 100, height: 100)
    }
}

var whiteCircleize:CGSize{
    switch type {
    case .TribeHomeCenter:
        return CGSize(width: 50, height: 50)
    case .TribeHomeOrbit:
        return CGSize(width: 40, height: 40)

    }
}

var profilePicSize:CGSize{
    switch type {
    case .TribeHomeCenter:
        return CGSize(width: 48, height: 48)
    case .TribeHomeOrbit:
        return CGSize(width: 38, height: 38)

    }
}

var titleLabelSize:CGSize{
    switch type {
    case .TribeHomeCenter:
        return CGSize(width: TribeView.viewSize(for: type).width, height: 20)
    case .TribeHomeOrbit:
        return CGSize(width: TribeView.viewSize(for: type).width, height: 20)
    }
}

var titleLabelTopConstant:CGFloat{
    switch type {
    case .TribeHomeCenter:
        return 80
    case .TribeHomeOrbit:
        return 60

    }
}

var titleFont:UIFont {
    switch type {
    case .TribeHomeCenter:
        return UIFont(name: UIFont.cineBeeRegular, size: 13)!
    case .TribeHomeOrbit:
        return UIFont(name: UIFont.cineBeeRegular, size: 11)!

    }

}

init(name:String,imageUrl:String?,type:CircleType) {
    self.type = type

    let tempSize = TribeView.viewSize(for: type)
    let origin = CGPoint(x: -tempSize.width/2, y: -tempSize.height/2)

   // super.init(frame: CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: size))
    super.init(frame: CGRect.init(origin: origin, size:tempSize))

    self.backgroundColor = UIColor.clear
    self.isUserInteractionEnabled = true
    addbackgroundImage()
    addWhiteCircle()
    addProfilePic(imageUrl: imageUrl)
    addName(name)
     addButton()
}

init(name:String,imageUrl:String?,size:CGSize,freeSizeLabel:Bool = false) {
    super.init(frame: CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: size))
    addProfilePic(imageUrl: imageUrl)
    addName(name)
}

private func addbackgroundImage() {
    bgImageView = UIImageView()
    bgImageView.contentMode = .scaleAspectFit
    bgImageView.translatesAutoresizingMaskIntoConstraints = false

    let width = NSLayoutConstraint(item: bgImageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: bgImagesize.width)

    let top = NSLayoutConstraint.init(item: bgImageView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: bgImageTopConstant)
    let height = NSLayoutConstraint.init(item: bgImageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:  bgImagesize.height)
    let centerx = NSLayoutConstraint.init(item: bgImageView, attribute: .centerX , relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)

    self.addSubview(bgImageView)
    self.addConstraints([width,height,top,centerx])
    bgImageView.image = UIImage(named: "GreenGlow")
   // bgImageView.clipsToBounds = true
   // bgImageView.layer.cornerRadius = bgImagesize.width/2

}

private func addWhiteCircle() {

    whiteCircle = UIView()
    whiteCircle.translatesAutoresizingMaskIntoConstraints = false
    whiteCircle.backgroundColor = UIColor.white
    bgImageView.addSubview(whiteCircle)

    let width = NSLayoutConstraint(item: whiteCircle, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: whiteCircleize.width)
    let height = NSLayoutConstraint.init(item: whiteCircle, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:  whiteCircleize.height)
    let centerx = NSLayoutConstraint.init(item: whiteCircle, attribute: .centerX , relatedBy: .equal, toItem: bgImageView, attribute: .centerX, multiplier: 1, constant: 0)
    let centery = NSLayoutConstraint.init(item: whiteCircle, attribute: .centerY , relatedBy: .equal, toItem: bgImageView, attribute: .centerY, multiplier: 1, constant: 0)

    self.addConstraints([width,height,centery,centerx])
    whiteCircle.layer.cornerRadius = whiteCircleize.width/2
    whiteCircle.clipsToBounds = true
}

private func addButton() {

   let button  = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.backgroundColor = UIColor.red
    bgImageView.isUserInteractionEnabled = true
    bgImageView.addSubview(button)

    let width = NSLayoutConstraint(item: button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: whiteCircleize.width)
    let height = NSLayoutConstraint.init(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:  whiteCircleize.height)
    let centerx = NSLayoutConstraint.init(item: button, attribute: .centerX , relatedBy: .equal, toItem: bgImageView, attribute: .centerX, multiplier: 1, constant: 0)
    let centery = NSLayoutConstraint.init(item: button, attribute: .centerY , relatedBy: .equal, toItem: bgImageView, attribute: .centerY, multiplier: 1, constant: 0)

    self.addConstraints([width,height,centery,centerx])
    button.layer.cornerRadius = whiteCircleize.width/2
    button.clipsToBounds = true

    button.addTarget(self, action: #selector(TribeView.circleTapAction), for: .touchUpInside)
    self.isUserInteractionEnabled = true
    button.isUserInteractionEnabled = true
}


private func addProfilePic(imageUrl:String?) {
    profilePic = AvatarImageView()
    profilePic.contentMode = .center
    profilePic.translatesAutoresizingMaskIntoConstraints = false
    whiteCircle.addSubview(profilePic)
    whiteCircle.isUserInteractionEnabled = true

    let width = NSLayoutConstraint(item: profilePic, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: profilePicSize.width)
    let height = NSLayoutConstraint.init(item: profilePic, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:  profilePicSize.height)
    let centerx = NSLayoutConstraint.init(item: profilePic, attribute: .centerX , relatedBy: .equal, toItem: whiteCircle, attribute: .centerX, multiplier: 1, constant: 0)
    let centery = NSLayoutConstraint.init(item: profilePic, attribute: .centerY , relatedBy: .equal, toItem: whiteCircle, attribute: .centerY, multiplier: 1, constant: 0)

    self.addConstraints([width,height,centery,centerx])
    profilePic.layer.cornerRadius = profilePicSize.width/2
    profilePic.clipsToBounds = true

    self.addConstraints([width,height,centery,centerx])
    profilePic.showImage(imageUrl)
    profilePic.isUserInteractionEnabled = true
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(TribeView.circleTapAction))
    tapGesture.numberOfTapsRequired = 1
    profilePic.addGestureRecognizer(tapGesture)

}

private func addName(_ name:String) {
    titleLabel = UILabel()
    titleLabel.translatesAutoresizingMaskIntoConstraints = false
    titleLabel.textAlignment = .center
    titleLabel.text = name
    titleLabel.font = titleFont
    titleLabel.textColor = UIColor.black
    self.addSubview(titleLabel)

    let top = NSLayoutConstraint.init(item: titleLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: titleLabelTopConstant)

    let width = NSLayoutConstraint(item: titleLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: titleLabelSize.width)
    let height = NSLayoutConstraint.init(item: titleLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:  titleLabelSize.height)
    let centerx = NSLayoutConstraint.init(item: titleLabel, attribute: .centerX , relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
    self.addConstraints([width,height,centerx,top])
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func addAnimation(path:UIBezierPath , time:Int) {
    let animation = CAKeyframeAnimation(keyPath: "position")
    animation.duration = CFTimeInterval(time)
    animation.isAdditive = true
    animation.repeatCount = Float.infinity
    //animation.calculationMode = kCAAnimationPaced
    //animation.rotationMode = kCAAnimationRotateAuto
   // animation.fillMode = kCAFillModeForwards
    animation.isRemovedOnCompletion = false
    animation.path = path.cgPath
    animation.speed = 1.0
    self.layer.add(animation, forKey: nil)
}

@objc func circleTapAction() {
    circleTapBlock?(self.index)
}
}

0 个答案:

没有答案