使用animateWithDuration在CGRect内移动UIImage

时间:2016-03-23 18:11:46

标签: ios swift uiimageview uiimage

所以我试图弄清楚如何使用animateWithDuration将CGRect内部绘制的UIImages移动到屏幕上的单元格中,但是我无法直观地看到编写代码的位置以及如何编写代码一个UIImage。我有一个CGRects数组,内容是在其中绘制的UIImage,我想同时移动所有图像。一旦他们到达最顶层的单元格,我希望它们出现在底部单元格中并重新开始。这是一张图片,可以更好地了解我在说什么:

DragMeToHell Screenshot

这是UIView和UIImages的drawRect代码:

覆盖func drawRect(rect:CGRect){         print(“drawRect:”)

    let context = UIGraphicsGetCurrentContext()!  // obtain graphics context
    // CGContextScaleCTM( context, 0.5, 0.5 )  // shrink into upper left quadrant
    let bounds = self.bounds          // get view's location and size
    let w = CGRectGetWidth( bounds )   // w = width of view (in points)
    let h = CGRectGetHeight( bounds ) // h = height of view (in points)
    self.dw = w/10.0                      // dw = width of cell (in points)
    self.dh = h/10.0                      // dh = height of cell (in points)

    print( "view (width,height) = (\(w),\(h))" )
    print( "cell (width,height) = (\(self.dw),\(self.dh))" )

    // draw lines to form a 10x10 cell grid
    CGContextBeginPath( context )               // begin collecting drawing operations
    for i in 1..<10 {
        // draw horizontal grid line
        let iF = CGFloat(i)
        CGContextMoveToPoint( context, 0, iF*(self.dh) )
        CGContextAddLineToPoint( context, w, iF*self.dh )
    }
    for i in 1..<10 {
        // draw vertical grid line
        let iFlt = CGFloat(i)
        CGContextMoveToPoint( context, iFlt*self.dw, 0 )
        CGContextAddLineToPoint( context, iFlt*self.dw, h )
    }
    UIColor.grayColor().setStroke()                        // use gray as stroke color
    CGContextDrawPath( context, CGPathDrawingMode.Stroke ) // execute collected drawing ops

    // establish bounding box for image
    let tl = self.inMotion ? CGPointMake( self.x, self.y )
                           : CGPointMake( CGFloat(row)*self.dw, CGFloat(col)*self.dh )
    let imageRect = CGRectMake(tl.x, tl.y, self.dw, self.dh)

    // place images in random cells
    //cellCoordinates = self.generateCoordinates()
    for xy in cellCoordinates {
        let randomImageRect = CGRectMake(xy.x, xy.y, self.dw, self.dh)
        let lavaImage : UIImage? = UIImage(named: "lava.png")
        lavaImage!.drawInRect(randomImageRect)
        imageCells.append(randomImageRect)
    }

    // place appropriate image where dragging stopped [EDITED]
    var img : UIImage?
    if ( self.col == 9 ) {
        img = UIImage(named:"otto.png")
    } else {
        img = UIImage(named:"angel.png")
    }
    img!.drawInRect(imageRect)

    // check for image intersection
    for tempImageRect in imageCells {
        if (CGRectIntersectsRect(imageRect, tempImageRect)) {
            img = UIImage(named:"devil.png")
            img!.drawInRect(imageRect)
            self.backgroundColor = UIColor.redColor()
        }
    }
}

0 个答案:

没有答案