2017-02-23 3 views
0

프로그래밍 방식으로 생성 한 UIButton이 있습니다. 실제로는 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; 
} 

스케일은 잘 작동한다. 움직이는 것은 객체의 크기를 변경할 때까지는 괜찮아 보입니다. 객체를 늘리면 손가락보다 느린 동작이되고, 객체보다 작 으면 객체가 손가락보다 빠르게 움직입니다. 왜 이런 식으로 작동합니까?

+1

크기를 조정 한 후 버튼 크기를 업데이트 해 보셨습니까? 그는 당신이 그것을 움직이려고 할 때 이전 크기에 응답하고 있을지도 모른다. –

+0

@Ricardo Alves, 나는이 같은 핑을 시도했다.'pinchView.frame = CGRectMake (bounds.origin.x, bounds.origin.y, bounds.origin.x + bounds.size.width, bounds.origin.y + bounds.size.height);'objScale'에서 그리고 스케일을 나눕니다 = ( – user5599807

답변

2

나는 어쩌면 그것이 작동, 당신은 다음과 같은 코드를 변경

- (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; 
} 
에 startPoint를하고 startCenter를 얻을한다고 생각합니다.

센터가 현재 센터이고, p는 현재 포인트이고, pPrev는 이전 포인트입니다. 현재 센터가 이전 포인트 이동 크기를 잘못 추가했습니다. 동적 거리가 아닌 상대 거리를 얻어야합니다.