var sp1Btn:UIButton? = nil; var customTimer: Timer? = nil; var testArray:NSMutableArray = NSMutableArray(capacity: 0) override func viewDidLoad() { super.viewDidLoad() let sp1Btn = UIButton(frame: CGRect(x: 260,y: 100,width: 38,height: 38)) sp1Btn.se
timage(UIImage(named: "sp1"),for: UIControlState.
normal) sp1Btn.addTarget(self,action:#selector(btnClick),for: UIControlEvents.touchUpInside) self.sp1Btn = sp1Btn; let array = ["好友","远见","举报"]; for i in 0 ..< 3 { let customeBtn: CustomButton = CustomButton(frame: CGRect(x: 260,height: 38)); customeBtn.setBackgroundImage(UIImage(named: "sp11"),for: UIControlState.
normal) customeBtn.setTitle(array[i],for: UIControlState.
normal) customeBtn.titleLabel?.font = UIFont.sy
stemFont(ofSize: 10) customeBtn.setTitleColor(UIColor.white,for: UIControlState.
normal) self.view.addSubview(customeBtn); customeBtn.startPoint = CGPoint(x: 278.5,y: 118.5); let point_x:CGFloat = (CGFloat)(20*i); let point_y:CGFloat = (CGFloat)(50*i); customeBtn.nearPoint = CGPoint(x: (sp1Btn.frame.origin.x-90 + point_x ),y: sp1Btn.frame.origin.y-40+point_y) customeBtn.endPoint = CGPoint(x: (sp1Btn.frame.origin.x-90 + point_x),y: sp1Btn.frame.origin.y-40+point_y) testArray.add(customeBtn) } self.view.addSubview(sp1Btn) } func btnClick() { var sy
stemSoundID:Sy
stemSoundID = 0; self.sp1Btn?.isSelected = !(self.sp1Btn?.isSelected)!; let path = Bundle.main.path(forResource: "composer_close",ofType: "wav") Au
dioServicesCreateSy
stemSoundID(NSURL.fileURL(withPath: path!) as CFURL,&sy
stemSoundID) Au
dioServicesPlayAlertSound(Sy
stemSoundID(sy
stemSoundID)) var i = 0; var timer = Timer.scheduledTimer(withTimeInterval: 0.1,repeats: true) { (parttimer) in i+=1; print("结果",i) if i==3 { parttimer.invalidate(); } let btn1:CustomButton = self.testArray[i-1] as! CustomButton; if (self.sp1Btn?.isSelected)! { let rotationAnimation = CAKeyframeAnimation(keyPath: "transform.rotation.z"); rotationAnimation.values = [NSNumber(value: 0.0),NSNumber(value: M_PI*2),NSNumber(value: 0)]; rotationAnimation.duration = 0.5; rotationAnimation.keyTimes = [NSNumber(value: 0),NSNumber(value: 4),NSNumber(value: 5)]; let position = CAKeyframeAnimation(keyPath: "position") position.duration = 0.5; let mutablePath = CGMutablePath(); mutablePath.move(to: btn1.startPoint!) mutablePath.addLine(to: btn1.nearPoint!) mutablePath.addLine(to: btn1.endPoint!); position.path = mutablePath; let animationGroup = CAAnimationGroup() animationGroup.animations = [position,rotationAnimation]; animationGroup.duration = 0.5; // animationGroup.isRemovedOnCompletion = false; animationGroup.fillMode = kCAFillModeForwards; animationGroup.timingFunction =
camediatimingFunction(name: k
camediatimingFunctionEaseIn) btn1.layer.add(animationGroup,forKey: nil); btn1.center = btn1.endPoint!; }else{ let rotationAnimation = CAKeyframeAnimation(keyPath: "transform.rotation.z"); rotationAnimation.values = [NSNumber(value: 0.0),NSNumber(value: 5)]; let position = CAKeyframeAnimation(keyPath: "position") position.duration = 0.5; let mutablePath = CGMutablePath(); mutablePath.move(to: btn1.endPoint!) mutablePath.addLine(to: btn1.nearPoint!) mutablePath.addLine(to: btn1.startPoint!); position.path = mutablePath; let animationGroup = CAAnimationGroup() animationGroup.animations = [position,forKey: nil); btn1.center = btn1.startPoint!; } } RunLoop.current.add(timer,forMode: RunLoopMode.commonModes) timer.fire() let animation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation") animation.duration = 0.2; if (self.sp1Btn?.isSelected)! { animation.fromValue = 0; animation.t
ovalue = -45; } else { animation.fromValue = -45; animation.t
ovalue = 0; } animation.isRemovedOnCompletion = false; animation.fillMode = kCAFillModeForwards; self.sp1Btn?.layer.add(animation,forKey: nil); }