2009-06-16 3 views
2

나는 터치를 감지하는 UIImageView의 하위 클래스를 만들고 터치를 기반으로 이미지를 이동, 회전 및 크기 조절합니다. 그러나, 나는 정말로 내가 여기의 바퀴를 재발 명하고있는 것처럼 느낀다. 그리고 그것은 나를 열중시킨다. 이미 어딘가에 존재해야하지 않습니까?uiimageview의 이동, 크기 조절 및 회전을위한 클래스

누구나 이미이 작업을 수행하고있는 클래스에 대한 예제 또는 링크가 있습니까? 또는 작성한 수업이 있다면 도움이 될 것입니다.

미리 감사드립니다.

답변

8

나는 그것을 알아 냈다 ... 나는 내 자신의 질문에 대답했다.

누군가에게 유용하기를 바랍니다.

관심있는 사람은 이미지를 이동, 크기 조정 및 회전하는 데 사용할 수있는 UIImageView 하위 클래스의 구현을 살펴 보겠습니다. 그것은 나를 위해 꽤 잘 작동합니다.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    if([touches count] == 1) { 

float difx = [[touches anyObject] locationInView:self].x - [[touches anyObject] previousLocationInView:self].x; 

float dify = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y; 



      CGAffineTransform newTransform1 = CGAffineTransformTranslate(self.transform, difx, dify); 

      self.transform = newTransform1; 

    } else  if([touches count] == 2) { 

int prevmidx = ([[[touches allObjects] objectAtIndex:0] previousLocationInView:self].x + [[[touches allObjects] objectAtIndex:1] previousLocationInView:self].x)/2; 

int prevmidy = ([[[touches allObjects] objectAtIndex:0] previousLocationInView:self].y + [[[touches allObjects] objectAtIndex:1] previousLocationInView:self].y)/2; 

int curmidx = ([[[touches allObjects] objectAtIndex:0] locationInView:self].x + [[[touches allObjects] objectAtIndex:1] locationInView:self].x)/2; 

int curmidy = ([[[touches allObjects] objectAtIndex:0] locationInView:self].y + [[[touches allObjects] objectAtIndex:1] locationInView:self].y)/2; 

      int difx = curmidx - prevmidx; 

      int dify = curmidy - prevmidy; 



CGPoint prevPoint1 = [[[touches allObjects] objectAtIndex:0] previousLocationInView:self]; 

CGPoint prevPoint2 = [[[touches allObjects] objectAtIndex:1] previousLocationInView:self]; 

CGPoint curPoint1 = [[[touches allObjects] objectAtIndex:0] locationInView:self]; 

CGPoint curPoint2 = [[[touches allObjects] objectAtIndex:1] locationInView:self]; 

      float prevDistance = [self distanceBetweenPoint1:prevPoint1 andPoint2:prevPoint2]; 

      float newDistance = [self distanceBetweenPoint1:curPoint1 andPoint2:curPoint2]; 

      float sizeDifference = (newDistance/prevDistance); 



      CGAffineTransform newTransform1 = CGAffineTransformTranslate(self.transform, difx, dify); 

      self.transform = newTransform1; 



      CGAffineTransform newTransform2 = CGAffineTransformScale(self.transform, sizeDifference, sizeDifference); 

      self.transform = newTransform2; 





      float prevAngle = [self angleBetweenPoint1:prevPoint1 andPoint2:prevPoint2]; 

      float curAngle = [self angleBetweenPoint1:curPoint1 andPoint2:curPoint2]; 

      float angleDifference = curAngle - prevAngle; 



      CGAffineTransform newTransform3 = CGAffineTransformRotate(self.transform, angleDifference); 

      self.transform = newTransform3; 

    } 

} 



- (NSInteger)distanceBetweenPoint1:(CGPoint)point1 andPoint2:(CGPoint)point2 { 

    CGFloat deltaX = fabsf(point1.x - point2.x); 

    CGFloat deltaY = fabsf(point1.y - point2.y); 

    CGFloat distance = sqrt((deltaY*deltaY)+(deltaX*deltaX)); 

    return distance; 

} 



- (CGFloat)angleBetweenPoint1:(CGPoint)point1 andPoint2:(CGPoint)point2 

{ 

    CGFloat deltaY = point1.y - point2.y; 

    CGFloat deltaX = point1.x - point2.x; 

    CGFloat angle = atan2(deltaY, deltaX); 

    return angle; 

}