イメージベースのアプリケーションを作成しています。作成したUIView内で回転/移動/拡大/縮小されたイメージを表示します。私はUIGestureを実装する方法についてのオンラインチュートリアルを続けました。これらを使用しているのが分かりました。これらの背後にあるコードを理解するだけで、このUIView内でジェスチャを保持できます。UIViewの外でGestureRecongnizersを使用して、UIImageViewを移動しないようにするにはどうすればよいですか?
-(void)scale:(id)sender {
NSLog(@"See a pinch gesture");
[self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
-(void)rotate:(id)sender {
NSLog(@"See a rotate gesture");
[self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
-(void)move:(id)sender {
NSLog(@"See a move gesture");
[[(UITapGestureRecognizer*)sender view] layer];//removeAllAnimations];
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 768) {
finalX = 768;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 1024) {
finalY = 1024;
}
}
else {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 1024) {
finalX = 768;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 768) {
finalY = 1024;
}
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
:
UIImage *image;
UIImageView *theImageView;
UIView *layoutOne;
ものは以下の私の宣言ジェスチャーRecognisersとしているが、私は自分の仕事を行うためにそれらを得るために続くチュートリアルの別の部分である
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
layoutOne = [[UIView alloc] initWithFrame:CGRectMake(95.0, 107.0, 578, 682)];
theImageView = [[UIImageView alloc] initWithFrame:[layoutOne frame]];
[theImageView setImage:image];
theImageView.userInteractionEnabled = TRUE;
[layoutOne addSubview:theImageView];
takePhoto.hidden = YES;
theImageView.clipsToBounds = YES;
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[theImageView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[theImageView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[theImageView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *deleteButton = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(delButton:)];
[deleteButton setNumberOfTapsRequired:2];
[deleteButton setDelegate:self];
[theImageView addGestureRecognizer:deleteButton];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[tapRecognizer requireGestureRecognizerToFail:deleteButton];
[theImageView addGestureRecognizer:tapRecognizer];
[self.view addSubview:theImageView];
}
注:ここでのコードです
申し訳ありませんコードの長い行については、私が達成しようとしていることを正確に理解しておきたいだけです。主に-(void)move:(id)sender
の中で行われている数学のことをよりよく理解したいので、UIImage/UIImageViewが私が作成したUIサブビュー(layoutOne)の外に移動するのを止めることができるかもしれません。あなたが手伝ってくれたら、本当に感謝します!ありがとう。