gpt4 book ai didi

ios - 在 iOS 中围绕枢轴点旋转 ImageView

转载 作者:搜寻专家 更新时间:2023-10-31 22:18:04 24 4
gpt4 key购买 nike

记录和旋转搜索栏

我下面有一个应用程序屏幕可以录制(最多 30 秒)音频。

  1. 如何在录制音频时平滑地旋转虚圆线上的小圆圈作为搜索栏?
  2. 如何在小圆圈沿线旋转时填充虚线。

enter image description here

谢谢。

最佳答案

Answer is having both ways to do so using a PanGesture and automatically using a Timer.

<强>1。使用 UIPanGestureRecognizer:

您可以使用 UIPanGestureRecognizer 实现这一点。circleView 是你的 mainViewnob 是另一个 viewimageView 将移到外面circleView

panGesture = UIPanGestureRecognizer(target: self, action: #selector(panHandler(_:)))
nob.addGestureRecognizer(panGesture)

panHandler(_ :)的定义

@objc func panHandler(_ gesture: UIPanGestureRecognizer) {
let point = gesture.location(in: self)
updateForPoints(point)
}

这是核心逻辑,它是如何工作的。

func updateForPoints(_ point: CGPoint) {

/*
* Parametric equation of circle
* x = a + r cos t
* y = b + r sin ⁡t
* a, b are center of circle
* t (theta) is angle
* x and y will be points which are on circumference of circle
*
270
|
_ | _
|
180 -------o------- 360
|
+ | +
|
90
*
*/

let centerOffset = CGPoint(x: point.x - circleView.frame.midX, y: point.y - circleView.frame.midY)

let a: CGFloat = circleView.center.x
let b: CGFloat = circleView.center.y
let r: CGFloat = circleView.layer.cornerRadius - 2.5
let theta: CGFloat = atan2(centerOffset.y, centerOffset.x)
let newPoints = CGPoint(x: a + r * cos(theta), y: b + r * sin(theta))

var rect = nob.frame
rect.origin = newPoints
nob.center = newPoints
}

<强>2。使用定时器自动移动

let totalSeconds: Int = 30 // You can change it whatever you want
var currentSecond: Int = 1
var timer: Timer?


func degreesToRadians(_ degree: CGFloat) -> CGFloat {
/// Will convert the degree (180°) to radians (3.14)
return degree * .pi / 180
}

func angleFromSeconds(_ seconds: Int) -> CGFloat {
/// Will calculate the angle for given seconds
let aSliceAngle = 360.0 / CGFloat(totalSeconds)
let angle = aSliceAngle * CGFloat(seconds) - 90
return angle
}

/// ------------- TIMER METHODS ------------- ///

func startTimer() {
stopTimer()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timeDidUpdate(_ :)), userInfo: nil, repeats: true)
timeDidUpdate(timer!)
}

func stopTimer() {
currentSecond = 1
timer?.invalidate()
timer = nil
}

@objc func timeDidUpdate(_ t: Timer) {
let angle = angleFromSeconds(currentSecond)
let theta = degreesToRadians(angle)
updateAngle(theta: theta, animated: true)
currentSecond += 1

if currentSecond > totalSeconds {
self.stopTimer()
}
}

/// --------------- MAIN METHOD -------------- ///
func updateAngle(theta: CGFloat, animated: Bool) {

let a: CGFloat = circleView.center.x
let b: CGFloat = circleView.center.y
let r: CGFloat = circleView.layer.cornerRadius
let newPoints = CGPoint(x: a + r * cos(theta), y: b + r * sin(theta))

var rect = nob.frame
rect.origin = newPoints

if animated {
UIView.animate(withDuration: 0.1, animations: {
self.nob.center = newPoints

/// Uncomment if your nob is not a circle and you want to maintain the angle too
// self.nob.transform = CGAffineTransform.identity.rotated(by: theta)
})
}
else {
nob.center = newPoints

/// Uncomment if your nob is not a circle and you want to maintain the angle too
//nob.transform = CGAffineTransform.identity.rotated(by: theta)
}
}

关于ios - 在 iOS 中围绕枢轴点旋转 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49919926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com