다른 접근 방법을 제안합니다. UIImageView
에 삽입 할 새 이미지를 끊임없이 만들지 마세요. 대신보기 계층을 다음과 같이 설정하십시오.
White view
"Hole" view (just a regular UIView)
Image view
즉, 흰색보기는 하위보기로 구멍보기를 갖습니다. 구멍보기에는 서브 뷰로 UIImageView
이 있습니다.
구멍보기에는 clipsToBounds
속성이 YES로 설정되어 있어야합니다 (코드 또는 펜촉으로 설정할 수 있음).
이미지보기의 크기는 이미지의 크기로 설정되어야합니다. 물론 이것은 홀 뷰의 크기보다 커야합니다.
그리고 이것은 매우 중요합니다. 이미지보기의 가운데는 구멍보기의 가운데로 설정해야합니다.
다음은 테스트 프로젝트에서 설정 한 코드입니다. 흰색보기는 self.view
입니다. 나는 흰색보기 중심에있는 구멍부터 시작하여 이미지보기의 중심을 구멍보기 중심으로 설정합니다.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
CGRect bounds = self.view.bounds;
self.holeView.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
self.holeView.clipsToBounds = YES;
bounds = self.holeView.bounds;
self.imageView.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
self.imageView.bounds = (CGRect){ CGPointZero, self.imageView.image.size };
}
또한 이미지보기의 크기를 이미지의 크기로 설정합니다. 흰색보기의 크기로 설정할 수 있습니다.
구멍을 화면 이동하고 회전하려면 holeView.transform
을 설정해야합니다. holeView.frame
또는 holeView.center
을 변경하지 않겠습니다. 변환을 계산하는 데 사용하는 _holeOffset
과 _holeRotation
이라는 두 개의 인스턴스 변수가 있습니다.
- (void)updateTransforms {
CGAffineTransform holeTransform = CGAffineTransformIdentity;
holeTransform = CGAffineTransformTranslate(holeTransform, _holeOffset.x, _holeOffset.y);
holeTransform = CGAffineTransformRotate(holeTransform, _holeRotation);
self.holeView.transform = holeTransform;
self.imageView.transform = CGAffineTransformInvert(holeTransform);
}
이 트릭 : 그것은 흰색보기를 통해 구멍처럼 보이는 이미지 만들기보기를 공개하는 비결은, 구멍 뷰의 변환의 효과를 취소, 이미지보기로 변환역을 적용하는 것입니다 하위 뷰의 중심이 슈퍼 뷰의 중심에있는 경우에만 하위 뷰에서 역변환을 사용합니다. (기술적으로 앵커 포인트가 정렬되어야하지만 기본적으로 뷰의 앵커 포인트가 가운데에 있습니다.)
holeView
에 UIPanGestureRecognizer
을 넣습니다.내보기 컨트롤러에 panGesture:
를 보내도록 구성 :
- (IBAction)panGesture:(UIPanGestureRecognizer *)sender {
CGPoint offset = [sender translationInView:self.view];
[sender setTranslation:CGPointZero inView:self.view];
_holeOffset.x += offset.x;
_holeOffset.y += offset.y;
[self updateTransforms];
}
가 나는 또한 holeView
에 UIRotationGestureRecognizer
을 넣어. 내보기 컨트롤러에 rotationGesture:
을 보내도록 구성했습니다.
- (IBAction)rotationGesture:(UIRotationGestureRecognizer *)sender {
_holeRotation += sender.rotation;
sender.rotation = 0;
[self updateTransforms];
}
감사합니다.이 코드는 훌륭합니다. 내 xcode에서 일하고있다. 나는 아직 그 곳을 늦게까지 소화하지는 않았지만 이것은 내가하려고했던 것을 정확히 달성한다. – user339946