2016-04-19 11 views
0

私はこの方法で1つのimageview.iイメージビューを含むメインビューコントローラ内にUIViewを持っています。イメージビューを動かすときにUIView内でイメージビューを保持する方法は?

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *aTouch = [touches anyObject]; 
    if (aTouch.view == self.sub_View) { 
    CGPoint location = [aTouch locationInView:self.sub_View]; 
    CGPoint previousLocation = [aTouch previousLocationInView:self.sub_View]; 
    self.imageView.frame = CGRectOffset(self.imageView.frame, (location.x - previousLocation.x), (location.y - previousLocation.y)); 
    } 
} 

Imageview move perfect。しかし、私は移動したときにUIView内にイメージビューを維持する必要があります。このコードの問題は、私はそれがまたUIViewの外に移動されるimageviewを移動するときです。私は、UIView内でイメージビューを保持し、UIView内でのみ移動する必要があります。 これを解決するのを手伝ってください。 imageviewの境界を設定してUIView内でのみ移動できるようにする方法

+0

それはそこにそのImageViewの滞在後に第一移動後に左上にImageViewのを保つImageViewの –

+0

ものです。 –

答えて

0

を移動するための休閑コードを使用することができますが、コードの下に試してみてください。

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *aTouch = [touches anyObject]; 
    if (aTouch.view == self.sub_View) { 
    CGPoint location = [aTouch locationInView:self.sub_View]; 
    CGPoint previousLocation = [aTouch previousLocationInView:self.sub_View]; 
    CGRect newFrame = CGRectOffset(self.imageView.frame, (location.x - previousLocation.x), (location.y - previousLocation.y)); 
    if(newFrame.origin.x < 0) 
     newFrame.origin.x = 0; 
    if(newFrame.origin.y < 0) 
     newFrame.origin.y = 0; 
    if(newFrame.origin.x + newFrame.size.width > self.frame.size.width) 
     newFrame.origin.x = self.frame.size.width - self.imageView.frame.size.width; 
    if(newFrame.origin.y + newFrame.size.height > self.frame.size.height) 
     newFrame.origin.y = self.frame.size.height - self.imageView.frame.size.height; 

    self.imageView.frame = newFrame; 

    } 
} 
+0

動きはUiview内部ですが、画像ビューをズームするとimagevuewフレームがリセットされ、スクロールすると画像は隠れます。 –

+0

self.frame.size.widthはどういう意味ですか? –

0

A)あなたは、あなたのUIView

YESにBの clipsToBoundsプロパティを設定することができます)あなたは、あなたの UIImageView

self.imageView.center = CGPointMake(MAX(0.0, MIN(location.x - previousLocation.x, self.sub_View.bounds.size.width)), MAX(0.0, MIN(location.y - previousLocation.y, self.sub_View.bounds.size.height))); 
+0

が含まれているメインビューにUIViewのあるself.sub_View –

関連する問題