簡體   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