我想使用UIGestureRecognizers旋轉,平移和縮放UIView。將識別器添加到超級視圖中,而旋轉,縮放等應用於子視圖(_baseImage),這是爲了使我能夠在未來接收手勢事件的同時在子視圖頂部覆蓋其他內容。使用UIGestureRecognizer(iOS)平移,縮放和旋轉UIView圍繞anchorPoint使用UIGestureRecognizer(iOS)
這個想法是,UIView應該縮放/旋轉兩個觸摸點下面的子視圖上的「錨點」,因爲這看起來是最自然的。我遇到的問題是在設置anchorPoint之後子視圖的位置,以及縮放和旋轉似乎沒有使用set anchorPoint。我對重疊座標系統/ CGAffine變換缺乏瞭解可能會讓我陷入困境。代碼來自各種示例。
這裏是我的代碼,因爲它代表的時刻:
-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
{
CGPoint oldOrigin = view.frame.origin;
view.layer.anchorPoint = anchorPoint;
CGPoint newOrigin = view.frame.origin;
CGPoint transition;
transition.x = newOrigin.x - oldOrigin.x;
transition.y = newOrigin.y - oldOrigin.y;
view.center = CGPointMake (view.center.x - transition.x, view.center.y - transition.y);
}
- (void) updateTransformWithOffset: (CGPoint) translation
{
// Create a blended transform representing translation,
// rotation, and scaling
_baseImage.transform = CGAffineTransformMakeTranslation(translation.x + tx, translation.y + ty);
_baseImage.transform = CGAffineTransformRotate(_baseImage.transform, theta);
_baseImage.transform = CGAffineTransformScale(_baseImage.transform, scale, scale);
}
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)uigr {
if (uigr.state == UIGestureRecognizerStateBegan) {
UIView *piece = self.view;
CGPoint locationInView = [uigr locationInView:_baseImage];
myFrame = _baseImage.frame;
CGPoint newAnchor = CGPointMake((locationInView.x/piece.bounds.size.width), (locationInView.y/piece.bounds.size.height));
[self setAnchorPoint:newAnchor forView:_baseImage];
}
}
- (void) handlePinch: (UIPinchGestureRecognizer *) uigr
{
[self adjustAnchorPointForGestureRecognizer:uigr];
if (uigr.state == UIGestureRecognizerStateBegan) {
initScale = scale;
}
scale = initScale*uigr.scale;
[self updateTransformWithOffset:CGPointZero];
}
是的,你會在我的代碼中看到我正在使用anchorPoint ...問題是我似乎無法讓它按預期工作。 – user3306929
現在我明白了。我剛剛看到大量代碼處理變換。 –
你的問題不是很精確,你的代碼相當複雜。我建議你嘗試將其分解成能夠很好地工作的碎片和有問題的碎片。然後刪除所有其他東西,直到您擁有最少量的代碼來重現您的問題。 –