J'ai une vue d'image que je veux pouvoir déplacer, et pince pour l'étirer. Tout fonctionne, mais c'est un peu nerveux quand je commence à faire des mouvements de pincement. La position va sauter d'avant en arrière entre les deux doigts.Déplacement en douceur et pincement de UIImageView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
startLocation = [[touches anyObject] locationInView:mouth_handle];
if([touches count] == 2) {
NSArray *twoTouches = [touches allObjects];
UITouch *first = [twoTouches objectAtIndex:0];
UITouch *second = [twoTouches objectAtIndex:1];
initialDistance = distanceBetweenPoints([first locationInView:mouth_handle],[second locationInView:mouth_handle]);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint pt = [[touches anyObject] locationInView:mouth_handle];
CGRect frame = [mouth_handle frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
frame.origin.x = (frame.origin.x < 58) ? 58 : frame.origin.x;
frame.origin.x = (frame.origin.x > (260 - mouth_handle.frame.size.width)) ? (260 - mouth_handle.frame.size.width) : frame.origin.x;
frame.origin.y = (frame.origin.y < 300) ? 300 : frame.origin.y;
frame.origin.y = (frame.origin.y > 377) ? 377 : frame.origin.y;
if(frame.origin.x - prevDistanceX > 2 && frame.origin.x - prevDistanceX < -2)
frame.origin.x = prevDistanceX;
if(frame.origin.y - prevDistanceY > 2 && frame.origin.y - prevDistanceY < -2)
frame.origin.y = prevDistanceY;
prevDistanceX = frame.origin.x;
prevDistanceY = frame.origin.y;
CGFloat handleWidth = mouth_handle.frame.size.width;
if([touches count] == 2) {
NSArray *twoTouches = [touches allObjects];
UITouch *first = [twoTouches objectAtIndex:0];
UITouch *second = [twoTouches objectAtIndex:1];
CGFloat currentDistance = distanceBetweenPoints([first locationInView:mouth_handle],[second locationInView:mouth_handle]);
handleWidth = mouth_handle.frame.size.width + (currentDistance - initialDistance);
handleWidth = (handleWidth < 60) ? 60 : handleWidth;
handleWidth = (handleWidth > 150) ? 150 : handleWidth;
if(initialDistance == 0) {
initialDistance = currentDistance;
}
initialDistance = currentDistance;
}
mouth_handle.frame = CGRectMake(frame.origin.x, frame.origin.y, handleWidth, 15);
}
Avez-vous des idées pour rendre ce processus plus fluide?
Je collerais l'image dans un UIWebView, et ne prendrais pas la peine de réinventer la fonctionnalité de zoom de pincement. –
Ne prenez pas le mot de Felix pour cela. Ce n'est certainement pas ce que les webviews sont pour. Vous devriez explorer 'UIScrollView'. –
Comme je l'ai mentionné dans un commentaire ci-dessous, je n'utilise pas UIScrollView parce que je veux juste modifier la largeur de l'image, et le faire pour seulement une partie de l'image. Si j'utilise un UIScrollView, il s'agirait de mettre à l'échelle l'image entière. Connaissez-vous un moyen d'utiliser UIScrollView et encore atteindre mon objectif ici? – Jacob