gpt4 book ai didi

ios - 可调整大小的居中圆形 UIView

转载 作者:行者123 更新时间:2023-12-01 17:51:30 25 4
gpt4 key购买 nike

我正在寻找一种创建 CIRCULAR UIView 的方法可调整大小并保持居中(见图)。本例中的圆形 View 是 UIView子类 WCSDailyGoal .

容器 View .m

- (void)createDailyGoalView
{
_dailyGoalView = [[WCSDailyGoalView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
_dailyGoalView.delegate = self;
_dailyGoalView.goal = 200;
_dailyGoalView.center = self.view.center;
[self.view addSubview:_dailyGoalView];
}

WCSDailyGoal.m
- (void)setGoal:(CGFloat)goal
{
CGPoint saveCenter = self.center;
CGRect newFrame = CGRectMake(self.frame.origin.x, self.frame.origin.y, goal, goal);
self.frame = newFrame;
self.layer.cornerRadius = goal / 2.0;
self.center = saveCenter;
}

enter image description here

最佳答案

我能够通过制作自定义 View 来实现这种效果(以下代码在 swift 中):

class ResizableCircleView: UIView {

var maxSize: CGFloat = 100
var minSize: CGFloat = 10

private var dragRecognizer: UIPanGestureRecognizer!
private var currentScale: CGFloat = 1

private var defaultSize: CGFloat { return frame.width / currentScale }

override func layoutSubviews() {
super.layoutSubviews()
if dragRecognizer == nil{
dragRecognizer = UIPanGestureRecognizer(target: self, action: "handleDrag:")
addGestureRecognizer(dragRecognizer)
}
backgroundColor = UIColor.blackColor()
layer.cornerRadius = frame.width / 2
clipsToBounds = true
}

func handleDrag(recognizer: UIPanGestureRecognizer){
let inTopArea = recognizer.locationInView(self).y < frame.height / 2
let dy = recognizer.translationInView(self).y

var newSize = frame.height + (inTopArea ? -1 : 1) * dy
newSize = min(maxSize, newSize)
newSize = max(minSize, newSize)
currentScale = newSize/defaultSize
transform = CGAffineTransformMakeScale(currentScale, currentScale)

recognizer.setTranslation(CGPointZero, inView: self)
}
}

您可以通过将所需值设置为 maxSize 来调整最大和最小尺寸。和 minSize .希望这会有所帮助。

关于ios - 可调整大小的居中圆形 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29107305/

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