在Swift中将图像保存在屏幕上

时间:2015-01-13 10:54:50

标签: swift uiswipegesturerecognizer

我有一个游戏,我使用UISwipeGestureRecognizer来移动屏幕。我试图找出如何使它,以便我刷卡的图像不能移出屏幕。我环顾互联网并找不到任何东西。

这是我的滑动方法:

func swipedRight(sender: UISwipeGestureRecognizer) {
   timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("moveBackground"), userInfo: nil, repeats: true)

   if imageView.frame.origin.x > 5000 {
      imageView.removeFromSuperview()
   }
}

这是我的moveBackground方法:

func moveBackground() {
    self.imageView.frame = CGRect(x: imageView.frame.origin.x - 100, y:     imageView.frame.origin.y, width: 5500, height: UIScreen.mainScreen().bounds.height)
}

到目前为止,我尝试过的唯一事情就是检查视图是否处于某个位置并删除它,如果它没有用。我将该代码添加到了滑动方法。

1 个答案:

答案 0 :(得分:0)

如果您在此处发布课程代码将非常有用,因此我们可以更具体地帮助您。

顺便说一下,每次调用函数时都要检查对象的位置,然后移动它,同时考虑它的大小。

让我们说你有一个纸牌游戏,你正在沿着棋盘刷卡。 为避免将卡从板上滑下(屏幕外),您必须执行以下操作:

// I declare the node for the card with an image
var card = SKSpriteNode(imageNamed: "card")

// I take its size from the image size itself
card.physicsBody = SKPhysicsBody(rectangleOfSize: card.size)

// I set its initial position to the middle of the screen and add it to the scene
card.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
self.addChild(card)

// I set the min and max positions for the card in both X and Y axis regarding the whole scene size.
// Fit it to your own bounds if different
let minX = card.size.width/2
let minY = card.size.height/2
let maxX = self.frame.size.width - card.size.height/2
let maxY = self.frame.size.width - card.size.height/2

// HERE GOES YOUR SWIPE FUNCTION
func swipedRight(sender: UISwipeGestureRecognizer) {
    // First we check the current position for the card
    if (card.position.x <= minX || card.position.x >= maxX || card.position.y <= minY || card.position.y >= maxY) {
        return
    }
    else {
        // ... 
        // YOUR CODE TO MOVE THE ELEMENT
        // ...
    }
}

我希望这有帮助! 问候。