如何获得UIView的坐标

时间:2013-12-31 10:16:59

标签: ios iphone ios6 uiimageview uipangesturerecognizer

在我的应用中,我使用PanGesture。

现在,我在主视图中有一个UIImageView,我可以在主视图中拖动我的UIImageView。

主视图中的哪个轴我必须拖动我的UIImageView?

4 个答案:

答案 0 :(得分:1)

尝试此操作来移动您的对象 或链接以获得最佳教程http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {

    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

if (recognizer.state == UIGestureRecognizerStateEnded) {

    CGPoint velocity = [recognizer velocityInView:self.view];
    CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
    CGFloat slideMult = magnitude / 200;
    NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);

    float slideFactor = 0.1 * slideMult; // Increase for more of a slide
    CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor), 
                                     recognizer.view.center.y + (velocity.y * slideFactor));
    finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
    finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);

    [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        recognizer.view.center = finalPoint;
    } completion:nil];

}

}

答案 1 :(得分:0)

你可以使用UIView类的frame属性。它给你视图的顶部,左侧,宽度和高度。

NSLog(@"view.frame.origin.x : %f",view.frame.origin.x);
NSLog(@"view.frame.origin.y : %f",view.frame.origin.y);
NSLog(@"view.frame.size.width : %f",view.frame.size.width);
NSLog(@"view.frame.size.height : %f",view.frame.size.height)

答案 2 :(得分:0)

您可以使用以下API将您的积分从一个视图转换为另一个视图,反之亦然。

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;

答案 3 :(得分:0)

    - (IBAction)panGestureReceived:(UIPanGestureRecognizer *)gesture {

        if ([gesture state] == UIGestureRecognizerStateBegan) {
            myVarToStoreTheBeganPosition = [gesture locationInView:self.view];
        } else if ([gesture state] == UIGestureRecognizerStateEnded) {
           CGPoint myNewPositionAtTheEnd = [gesture locationInView:self.view];
           // and now handle it ;)
        } else if ([gesture state] == UIGestureRecognizerStateChanged) {
           CGPoint myNewPositionAtTheEnd = [gesture locationInView:self.view];
           // and now handle it ;)
        }
    }