Question

I am working on a app and i want to move a UIImage in the Y axis only, not in Xaxis, here's what i did till now

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

}

The above code is moving the arrowImage in both the axis, stretchPoint is an instance of CGPoint, My app is in portrait mode, can anyone give me a basic idea to how to do this, as the arrowImage is a instance of UIImageView.

Was it helpful?

Solution

this should work fine:

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

OTHER TIPS

Subclass the UIImage and implement the following code

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


}

I am not sure as have not tested,but you can implement something like this :

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

Something like:

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top