简体   繁体   中英

Move a UIImageView in y axis

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.

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;


}

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM