gpt4 book ai didi

ios - UIButton-移动和缩放

转载 作者:行者123 更新时间:2023-12-01 19:56:24 24 4
gpt4 key购买 nike

我有一个以编程方式创建的UIButton。实际上,它不应该是UIButton,我只需要有可能在图像上方标记一些区域。

所以我需要的功能-移动对象并调整其大小。为此,我有2种方法:

- (void) objMove:(id) sender withEvent:(UIEvent *) event
{
UIControl *control = sender;
UITouch *t = [[event allTouches] anyObject];
CGPoint pPrev = [t previousLocationInView:control];
CGPoint p = [t locationInView:control];
CGPoint center = control.center;
center.x += p.x - pPrev.x;
center.y += p.y - pPrev.y;
control.center = center;
}

- (void)objScale:(UIPinchGestureRecognizer *)recognizer
{
UIView *pinchView = recognizer.view;
CGRect bounds = pinchView.bounds;
CGPoint pinchCenter = [recognizer locationInView:pinchView];
pinchCenter.x -= CGRectGetMidX(bounds);
pinchCenter.y -= CGRectGetMidY(bounds);
CGAffineTransform transform = pinchView.transform;
transform = CGAffineTransformTranslate(transform, pinchCenter.x, pinchCenter.y);
CGFloat scale = recognizer.scale;
transform = CGAffineTransformScale(transform, scale, scale);
transform = CGAffineTransformTranslate(transform, -pinchCenter.x, -pinchCenter.y);
pinchView.transform = transform;
recognizer.scale = 1.0;
}

规模工作正常。直到我更改对象的大小之前,移动看起来都还不错-当我增加对象的大小时,它变得比手指移动得慢,反之亦然-如果对象小于原始对象,则移动得比手指快。为什么会这样?

最佳答案

我认为您应该在以下位置获得startPoint和startCenter

 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// get startPoint and startCenter here
}

- (void) objMove:(id) sender withEvent:(UIEvent *) event
{
UIControl *control = sender;
UITouch *t = [[event allTouches] anyObject];
CGPoint p = [t locationInView:control];
startCenter.x += p.x - startPoint.x;
startCenter.y += p.y - startPoint.y;
control.center = startCenter;
}

像这样更改您的代码,也许可以。

您的中心是当前中心,p是当前点,pPrev是上一个点。
当前中心加上上一点移动的大小是错误的。
您应该获得相对距离,而不是动态距离。

关于ios - UIButton-移动和缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42422070/

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