繁体   English   中英

在y轴上移动UIImageView

[英]Move a UIImageView in y axis

我正在开发应用程序,我只想在Y轴上移动UIImage,而不要在Xaxis上移动,这是我到目前为止所做的

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

}

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

子类化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;


}

这应该工作正常:

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

我不确定还没有测试过,但是您可以执行以下操作:

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

就像是:

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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