Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何重复函数并每次更改变量?_Ios_Swift - Fatal编程技术网

Ios 如何重复函数并每次更改变量?

Ios 如何重复函数并每次更改变量?,ios,swift,Ios,Swift,我尝试多次重复函数createCircle(),每次我想更改button.center值时,如何才能做到这一点,同时仍保留每个圆的动画?当前,当我尝试通过复制和粘贴具有不同位置的函数createCircle()来重复变量时,我的动画handleTap()将无法在其他圆上工作 import UIKit class SecondViewController: UIViewController { let shapeLayer = CAShapeLayer() l

我尝试多次重复函数createCircle(),每次我想更改button.center值时,如何才能做到这一点,同时仍保留每个圆的动画?当前,当我尝试通过复制和粘贴具有不同位置的函数createCircle()来重复变量时,我的动画handleTap()将无法在其他圆上工作

import UIKit

class SecondViewController: UIViewController {
    
    let shapeLayer = CAShapeLayer()
    
    let percentageLabel: UILabel = {
        let label = UILabel()
        label.text = ""
        label.textAlignment = .center
        label.font = UIFont.boldSystemFont(ofSize: 28)
        label.textColor = UIColor(red: 0.59, green: 0.42, blue: 0.23, alpha: 1.00)
        return label
        
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        createCircle()
        
    }
    func createCircle() {
        let trackLayer = CAShapeLayer()
        
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        button.layer.cornerRadius = 0.5 * button.bounds.size.width
        button.clipsToBounds = true
        button.center = view.center
        button.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
        view.addSubview(button)

        let circularPath = UIBezierPath(arcCenter: .zero, radius: 50, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)
        
        trackLayer.path = circularPath.cgPath
        
        trackLayer.strokeColor = UIColor(red: 0.82, green: 0.69, blue: 0.52, alpha: 1.00).cgColor
        trackLayer.fillColor = UIColor.clear.cgColor
        trackLayer.lineWidth = 10
        trackLayer.position = view.center
        view.layer.addSublayer(trackLayer)
        
        shapeLayer.path = circularPath.cgPath
        
        shapeLayer.strokeColor = UIColor(red: 0.59, green: 0.42, blue: 0.23, alpha: 1.00).cgColor
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.lineWidth = 10
        shapeLayer.lineCap = CAShapeLayerLineCap.round
        shapeLayer.position = view.center
        shapeLayer.transform = CATransform3DMakeRotation(-CGFloat.pi / 2, 0, 0, 1)
        
        view.addSubview(percentageLabel)
        
        percentageLabel.frame = CGRect(x: 0, y: 0, width: 150, height: 150)
        percentageLabel.center = view.center
    }
    var done = 0
    var toDo = 0
    
    @objc func handleTap() {
        toDo = 5
        if done < toDo {
            done += 1
        } else {
            done -= toDo
        }
        let percentage = CGFloat(done) / CGFloat(toDo)
        percentageLabel.text = "\(Int(percentage * 100))%"
        
        DispatchQueue.main.async {
            self.shapeLayer.strokeEnd = percentage
        }
        view.layer.addSublayer(shapeLayer)
    }
}
导入UIKit
类SecondViewController:UIViewController{
设shapeLayer=CAShapeLayer()
let percentageLabel:UILabel={
let label=UILabel()
label.text=“”
label.textAlignment=.center
label.font=UIFont.boldSystemFont(大小:28)
label.textColor=UIColor(红色:0.59,绿色:0.42,蓝色:0.23,阿尔法:1.00)
退货标签
}()
重写func viewDidLoad(){
super.viewDidLoad()
createCircle()
}
func createCircle(){
让trackLayer=CAShapeLayer()
let button=ui按钮(类型:。自定义)
button.frame=CGRect(x:0,y:0,宽度:100,高度:100)
button.layer.cornerRadius=0.5*button.bounds.size.width
button.clipstobunds=true
button.center=view.center
addTarget(self,action:#选择器(handleTap),用于:。touchUpInside)
view.addSubview(按钮)
设circularPath=UIBezierPath(弧心:.0,半径:50,星形缠结:0,端角:2*CGFloat.pi,顺时针:true)
trackLayer.path=circularPath.cgPath
trackLayer.strokeColor=UIColor(红色:0.82,绿色:0.69,蓝色:0.52,alpha:1.00)
trackLayer.fillColor=UIColor.clear.cgColor
trackLayer.lineWidth=10
trackLayer.position=view.center
view.layer.addSublayer(trackLayer)
shapeLayer.path=circularPath.cgPath
shapeLayer.strokeColor=UIColor(红色:0.59,绿色:0.42,蓝色:0.23,alpha:1.00)
shapeLayer.fillColor=UIColor.clear.cgColor
shapeLayer.lineWidth=10
shapeLayer.lineCap=CAShapeLayerLineCap.round
shapeLayer.position=view.center
shapeLayer.transform=CATTransformerM3dMakeRotation(-CGFloat.pi/2,0,0,1)
view.addSubview(百分比标签)
percentageLabel.frame=CGRect(x:0,y:0,宽度:150,高度:150)
percentageLabel.center=view.center
}
var done=0
var toDo=0
@objc func handleTap(){
toDo=5
如果完成
如果要在不同调用中仅更改函数体的一部分,则应使用参数。至于点击检测,我相信它发生在视图中,而不是在层中(不确定)。在点击检测的情况下,它确实会影响视图,而不是后期,我如何才能将其更改为影响单个圆?对于检测,我相信这个问题可能会帮助您:我并不是试图赋予每个圆它自己的属性。我只希望能够在每个圆上单独使用相同的函数handleTap()。正如你提到的,这似乎影响了观点。例如,当我单击我打印的三个按钮中的一个时,动画将仅在最近调用的按钮上播放。这与之前的不一样吗?