ios - 在捏合手势的中心发出缩放层
<p><p>我目前在图层中有一个 map (tilemap),我想使用以下代码进行平移/缩放:</p>
<pre><code>- (void) pinchGestureUpdated: (UIPinchGestureRecognizer *) recognizer {
if( == UIGestureRecognizerStateBegan) {
_lastScale = ;
CGPoint touchLocation1 = ;
CGPoint touchLocation2 = ;
CGPoint centerGL = [ convertToGL: ccpMidpoint(touchLocation1, touchLocation2)];
_pinchCenter = ;
}
else if ( == UIGestureRecognizerStateChanged) {
// NSLog(@"%d", recognizer.scale);
CGFloat newDeltaScale = 1 -(_lastScale - );
newDeltaScale = MIN(newDeltaScale, kMaxScale / self.scale);
newDeltaScale = MAX(newDeltaScale, kMinScale / self.scale);
CGFloat newScale = self.scale * newDeltaScale;
//self.scale = newScale;
;
_lastScale = ;
}
}
- (void) scale: (CGFloat) newScale atCenter: (CGPoint) center {
CGPoint oldCenterPoint = ccp(center.x * self.scale, center.y * self.scale);
// Set the scale.
self.scale = newScale;
// Get the new center point.
CGPoint newCenterPoint = ccp(center.x * self.scale, center.y * self.scale);
// Then calculate the delta.
CGPoint centerPointDelta= ccpSub(oldCenterPoint, newCenterPoint);
// Now adjust your layer by the delta.
self.position = ccpAdd(self.position, centerPointDelta);
}
</code></pre>
<p>我的问题是缩放没有在夹点的中心生效,即使我试图在通过这种方法放大的同时改变它:<code>(void) scale:(CGFloat ) newScale atCenter:(CGPoint)中心</code>。有什么原因可能无法正常发生吗?另外,如何将夹点的中心位置转换为我的场景/图层的坐标系?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我的方法实际上一切都很好。我遇到的问题是层 <code>anchor point</code> 与我定义的 map 不同,它在缩放期间引入了偏移。我必须将两个 anchor 都设置为 <code>ccp(0,0)</code>。</p>
<p>捏合手势中心的屏幕坐标到图层的转换是正确的,在使用UIKIt <code>手势识别器</code>时可以通过以下说明实现:</p>
<pre><code>CGPoint centerGL = [ convertToGL: ccpMidpoint(touchLocation1, touchLocation2)];
_pinchCenter = ;
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在捏合手势的中心发出缩放层,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/19049545/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/19049545/
</a>
</p>
页:
[1]