在y轴上移动UIImageView

时间:2011-01-21 10:20:01

标签: iphone objective-c xcode touchesmoved

我正在开发一个应用程序,我只想在Y轴上移动UIImage,而不是在Xaxis中移动,这就是我现在所做的事情

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 stretchPoint = [[touches anyObject]locationInView:self.view];
 arrowImage.center = stretchPoint;

}

上面的代码是在两个轴上移动arrowImage, stretchPoint是CGPoint的一个实例,我的应用程序处于纵向模式,任何人都可以给我一个如何做到这一点的基本想法,因为arrowImage是UIImageView的一个实例。

4 个答案:

答案 0 :(得分:1)

这应该可以正常工作:

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];

        CGPoint currentPoint = [touch locationInView:self.view];
        currentPoint.x = arrowImage.center.x;

        arrowImage.center = currentPoint;
    }

答案 1 :(得分:1)

对UIImage进行子类化并实现以下代码

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:[self superview]];


    CGPoint newCenter = CGPointMake(self.center.x, currentPoint.y);
    self.center = newCenter;


}

答案 2 :(得分:0)

我不确定没有测试过,但你可以实现这样的东西:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y = 20;
}

答案 3 :(得分:0)

类似的东西:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    stretchPoint = [[touches anyObject] locationInView:self.view];
    arrowImage.frame = CGPointMake(arrowImage.origin.x, 
                                   stretchPoint.y, 
                                   arrowImage.size.width,
                                   arrowImage.size.height);

}