我有一个UIButton
我以编程方式创建。实际上它应该是UIButton
,我只需要有可能在图像上方标记一些区域。
所以我需要它的功能 - 移动对象并调整它的大小。为此,我有两种方法:
- (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;
}
比例正常。移动看起来没问题,直到我改变对象的大小 - 当我增加对象时,它变得比手指慢,反之亦然 - 如果对象小于原始对象,它比手指移动得快。为什么它会像这样?
答案 0 :(得分:2)
我认为你应该在
中获得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是前一点。 当前中心添加前一点移动大小是错误的。 你应该得到相对距离,而不是动态距离。