Answer: Michael Fredrickson (Best answer) It's easy to achieve the zoom effect, just set the scale attribute of the game's main layer, but there are still some details that need to be paid attention to. When scaling the layer, the position of the layer will be increased as a whole. If you want to achieve the scrolling effect in the game, you need to make the following settings: You can set the anchorPoint in the layer to ccp(0.0f, 0.0f), then calculate the distance the layer rises, and then reset the parameters to return it to its original state.
- (void) scale:(CGFloat) newScale scaleCenter:(CGPoint) scaleCenter { // scaleCenter is the point to zoom to.. // If you are doing a pinch zoom, this should be the center of your pinch. // Get the original center point. CGPoint oldCenterPoint = ccp(scaleCenter.x * yourLayer.scale, scaleCenter.y * yourLayer.scale); // Set the scale. yourLayer.scale = newScale; // Get the new center point. CGPoint newCenterPoint = ccp(scaleCenter.x * yourLayer.scale, scaleCenter.y * yourLayer.scale); // Then calculate the delta. CGPoint centerPointDelta = ccpSub(oldCenterPoint, newCenterPoint); // Now adjust your layer by the delta. yourLayer.position = ccpAdd(yourLayer.position, centerPointDelta); }
Implementing Pinch zoom is very simple, just detect touchesMoved and then call the scaling routine.
- (void) ccTouchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { // Examine allTouches instead of just touches. Touches tracks only the touch that is currently moving... // But stationary touches still trigger a multi-touch gesture. NSArray* allTouches = [[event allTouches] allObjects]; if ([allTouches count] == 2) { // Get two of the touches to handle the zoom UITouch* touchOne = [allTouches objectAtIndex:0]; UITouch* touchTwo = [allTouches objectAtIndex:1]; // Get the touches and previous touches. CGPoint touchLocationOne = [touchOne locationInView: [touchOne view]]; CGPoint touchLocationTwo = [touchTwo locationInView: [touchTwo view]]; CGPoint previousLocationOne = [touchOne previousLocationInView: [touchOne view]]; CGPoint previousLocationTwo = [touchTwo previousLocationInView: [touchTwo view]]; // Get the distance for the current and previous touches. CGFloat currentDistance = sqrt( pow(touchLocationOne.x - touchLocationTwo.x, 2.0f) + pow(touchLocationOne.y - touchLocationTwo.y, 2.0f)); CGFloat previousDistance = sqrt( pow(previousLocationOne.x - previousLocationTwo.x, 2.0f) + pow(previousLocationOne.y - previousLocationTwo.y, 2.0f)); // Get the delta of the distances. CGFloat distanceDelta = currentDistance - previousDistance; // Next, position the camera to the middle of the pinch. // Get the middle position of the pinch. CGPoint pinchCenter = ccpMidpoint(touchLocationOne, touchLocationTwo); // Then, convert the screen position to node space... use your game layer to do this. pinchCenter = [yourLayer convertToNodeSpace:pinchCenter]; // Finally, call the scale method to scale by the distanceDelta, pass in the pinch center as well. // Also, multiply the delta by PINCH_ZOOM_MULTIPLIER to slow down the scale speed. // A PINCH_ZOOM_MULTIPLIER of 0.005f works for me, but experiment to find one that you like. [self scale:yourlayer.scale - (distanceDelta * PINCH_ZOOM_MULTIPLIER) scaleCenter:pinchCenter]; } }
Answer: Andrew If all elves come from the same parent, you can scale them to coordinate different parents and ensure the correlation between them.
Answer: mohammad alabid The following code can scale the position of the layer by 2 times,
Answer: Michael Fredrickson
(Best answer)
It's easy to achieve the zoom effect, just set the scale attribute of the game's main layer, but there are still some details that need to be paid attention to.
When scaling the layer, the position of the layer will be increased as a whole. If you want to achieve the scrolling effect in the game, you need to make the following settings:
You can set the anchorPoint in the layer to ccp(0.0f, 0.0f), then calculate the distance the layer rises, and then reset the parameters to return it to its original state.
Implementing Pinch zoom is very simple, just detect touchesMoved and then call the scaling routine.
Answer: Andrew
If all elves come from the same parent, you can scale them to coordinate different parents and ensure the correlation between them.
Answer: mohammad alabid
The following code can scale the position of the layer by 2 times,