加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift实现弹出菜单

发布时间:2020-12-14 06:20:04 所属栏目:百科 来源:网络整理
导读: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))
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.setImage(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.systemFont(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 systemSoundID:SystemSoundID = 0; self.sp1Btn?.isSelected = !(self.sp1Btn?.isSelected)!; let path = Bundle.main.path(forResource: "composer_close",ofType: "wav") AudioServicesCreateSystemSoundID(NSURL.fileURL(withPath: path!) as CFURL,&systemSoundID) AudioServicesPlayAlertSound(SystemSoundID(systemSoundID)) 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: kCAMediaTimingFunctionEaseIn) 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.toValue = -45; } else { animation.fromValue = -45; animation.toValue = 0; } animation.isRemovedOnCompletion = false; animation.fillMode = kCAFillModeForwards; self.sp1Btn?.layer.add(animation,forKey: nil); }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读