2017-09-27 3 views
0

app.How에서 특정 값을 전달하여 하위보기 내에서 이미지를 이동하려고하면보기의 "CGPoints"픽셀 포인트를 계산합니다.이 최선의 방법은 무엇입니까.보기 내에서 이미지를 이동하는 방법

setFrame:CGRectMake(_imgstride.frame.origin.x+_imgstride.frame.size.width, 
_imgstride.frame.origin.y, _imgstride.frame.size.width, 
_imgstride.frame.size.height)]; 

위의 코드를 사용해 보았습니다. imageView는 움직이지만 원하는 방식으로 작동하지 않습니다.

+0

이미지 뷰가 이동하지만, 작동하지 않는 - 작동 수단 하나도 –

답변

0

시도해보십시오. 스위프트를 들어

:보기 me.imageview 이동에 대한

그 작업 할 수 있습니다. 목표 C 용

class ViewController: UIViewController { 

    @IBOutlet weak var imageView: UIImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let myPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(myPanAction)) 
     myPanGestureRecognizer.minimumNumberOfTouches = 1 
     myPanGestureRecognizer.maximumNumberOfTouches = 1 
     self.imageView.addGestureRecognizer(myPanGestureRecognizer) 
    } 

    func myPanAction(recognizer: UIPanGestureRecognizer) { 
     let translation = recognizer.translation(in: self.view) 
     var recognizerFrame = recognizer.view?.frame 
     recognizerFrame?.origin.x += translation.x 
     recognizerFrame?.origin.y += translation.y 

     if (self.view.bounds.contains(recognizerFrame!)) { 
      recognizer.view?.frame = recognizerFrame! 
     }else{ 
      if (recognizerFrame?.origin.y)! < self.view.bounds.origin.y { 
       recognizerFrame?.origin.y = 0 
      }else if ((recognizerFrame?.origin.y)! + (recognizerFrame?.size.height)! > self.view.bounds.size.height) { 
       recognizerFrame?.origin.y = self.view.bounds.size.height - (recognizerFrame?.size.height)! 
      } 

      if (recognizerFrame?.origin.x)! < self.view.bounds.origin.x { 
       recognizerFrame?.origin.x = 0 
      }else if ((recognizerFrame?.origin.x)! + (recognizerFrame?.size.width)! > self.view.bounds.size.width) { 
       recognizerFrame?.origin.x = self.view.bounds.size.width - (recognizerFrame?.size.width)! 
      } 
     } 
     recognizer.setTranslation(CGPoint(x: 0, y: 0), in: self.view) 

    } 
} 

:

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; 
    [self.imageview addGestureRecognizer:pan]; 
} 
#pragma mark - Gesture Recognizer 
-(void)handlePan:(UIPanGestureRecognizer *)gesture { 

    CGPoint translation = [gesture translationInView:self.view]; 
    CGRect recognizerFrame = gesture.view.frame; 
    recognizerFrame.origin.x += translation.x; 
    recognizerFrame.origin.y += translation.y; 

    if (CGRectContainsRect(self.view.bounds, recognizerFrame)) { 

     gesture.view.frame = recognizerFrame; 
    }else { 

     if (recognizerFrame.origin.y < self.view.bounds.origin.y) { 
      recognizerFrame.origin.y = 0; 
     }else if (recognizerFrame.origin.y + recognizerFrame.size.height > self.view.bounds.size.height) { 
      recognizerFrame.origin.y = self.view.bounds.size.height - recognizerFrame.size.height; 
     } 

     if (recognizerFrame.origin.x < self.view.bounds.origin.x) { 
      recognizerFrame.origin.x = 0; 
     }else if (recognizerFrame.origin.x + recognizerFrame.size.width > self.view.bounds.size.width) { 
      recognizerFrame.origin.x = self.view.bounds.size.width - recognizerFrame.size.width; 
     } 
    } 
    [gesture setTranslation:CGPointMake(0, 0) inView:self.view]; 

} 

@end 
관련 문제