我目前在图层中有一个 map (tilemap),我想使用以下代码进行平移/缩放:
- (void) pinchGestureUpdated: (UIPinchGestureRecognizer *) recognizer {
if([recognizer state] == UIGestureRecognizerStateBegan) {
_lastScale = [recognizer scale];
CGPoint touchLocation1 = [recognizer locationOfTouch:0 inView:recognizer.view];
CGPoint touchLocation2 = [recognizer locationOfTouch:1 inView:recognizer.view];
CGPoint centerGL = [[CCDirector sharedDirector] convertToGL: ccpMidpoint(touchLocation1, touchLocation2)];
_pinchCenter = [self convertToNodeSpace:centerGL];
}
else if ([recognizer state] == UIGestureRecognizerStateChanged) {
// NSLog(@"%d", recognizer.scale);
CGFloat newDeltaScale = 1 - (_lastScale - [recognizer scale]);
newDeltaScale = MIN(newDeltaScale, kMaxScale / self.scale);
newDeltaScale = MAX(newDeltaScale, kMinScale / self.scale);
CGFloat newScale = self.scale * newDeltaScale;
//self.scale = newScale;
[self scale: newScale atCenter:_pinchCenter];
_lastScale = [recognizer scale];
}
}
- (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);
}
我的问题是缩放没有在夹点的中心生效,即使我试图在通过这种方法放大的同时改变它:(void) scaleCGFloat ) newScale atCenterCGPoint)中心 。有什么原因可能无法正常发生吗?另外,如何将夹点的中心位置转换为我的场景/图层的坐标系?
Best Answer-推荐答案 strong>
我的方法实际上一切都很好。我遇到的问题是层 anchor point 与我定义的 map 不同,它在缩放期间引入了偏移。我必须将两个 anchor 都设置为 ccp(0,0) 。
捏合手势中心的屏幕坐标到图层的转换是正确的,在使用UIKIt 手势识别器 时可以通过以下说明实现:
CGPoint centerGL = [[CCDirector sharedDirector] convertToGL: ccpMidpoint(touchLocation1, touchLocation2)];
_pinchCenter = [self convertToNodeSpace:centerGL];
关于ios - 在捏合手势的中心发出缩放层,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/19049545/
|