2011-01-21 90 views
0

我工作的一个应用程序,我想在只有Y轴移动一个UIImage,而不是在X轴,这里就是我所做的到现在移动一个UIImageView在Y轴

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

} 

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

回答

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; 
    } 
+0

但他不想在这里更改X值。 – Sarah 2011-01-21 11:22:38

0

我不知道因为没有测试过,但你可以实现这样的事情:

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

现在currentPoint设置为20,然后会发生什么,代码是不完整的 – Radix 2011-01-28 08:42:10

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); 

} 
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; 


}