iOS开发悬浮提词器

  1. 创建一个新的UIWindow并将其添加到应用程序的主窗口上方:

let window = UIWindow(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))

window.windowLevel = .statusBar + 1

window.backgroundColor = UIColor.clear

window.makeKeyAndVisible()

UIApplication.shared.windows.first?.addSubview(window)

  1. 创建一个UILabel来显示文本内容,并设置相关属性:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))

label.text = "这里是提词器的文本内容"

label.font = UIFont.systemFont(ofSize: 16)

label.textColor = UIColor.white

label.numberOfLines = 0

label.textAlignment = .center

self.view.addSubview(label)

  1. 实现拖动和缩放悬浮窗口的功能:

@IBAction func panGestureAction(_ sender: UIPanGestureRecognizer) {

    let point = sender.translation(in: self.view.superview)

    sender.setTranslation(CGPoint.zero, in: self.view.superview)

    var center = self.view.center

    center.x += point.x

    center.y += point.y

    self.view.center = center

}

@IBAction func pinchGestureAction(_ sender: UIPinchGestureRecognizer) {

    if sender.state == .began || sender.state == .changed {

        let scale = sender.scale

        self.view.transform = self.view.transform.scaledBy(x: scale, y: scale)

        sender.scale = 1.0

    }

}

  1. 使用Auto Layout进行布局:

self.view.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([

    self.view.topAnchor.constraint(equalTo: self.view.superview!.topAnchor),

    self.view.leadingAnchor.constraint(equalTo: self.view.superview!.leadingAnchor),

    self.view.trailingAnchor.constraint(equalTo: self.view.superview!.trailingAnchor),

    self.view.bottomAnchor.constraint(equalTo: self.view.superview!.bottomAnchor)

])

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值