2013-02-20 96 views
1

我有一个UIView子类,包含几个子视图,我想拖动&拖放到UICollectionView中包含的其他UIViews之一。当拖动开始时,我想将拖动的视图从其当前的大小缩放到拖动持续时间内的较小值(原始大小过大,以至于无法首先缩放它时方便地选择放置目标)到目前为止,我有这样的:IOS拖放与缩放

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self.superview bringSubviewToFront:self]; 
    self.transform = CGAffineTransformMakeScale(0.3f, 0.3f); 
    startLocation = ([[touches anyObject] locationInView:self]); 
    startLocation = CGPointApplyAffineTransform(startLocation, self.transform); 
} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGPoint pt = [[touches anyObject] locationInView:self]; 
    float dx = pt.x - startLocation.x; 
    float dy = pt.y - startLocation.y; 
    CGPoint newCenter = CGPointApplyAffineTransform (CGPointMake(self.center.x + dx, self.center.y + dy), self.transform); 
    self.center = newCenter; 
} 

这是一个开始,因为它缩放拖动的UIView,我想它,并让我将它拖到;但是,拖动的UIView不会直接用鼠标指针移动(我在模拟器上运行)。当图像靠近模拟器屏幕的左上角时,鼠标指针&拖动视图在一起;但是当我离开屏幕的右上角时,视图不再直接用鼠标指针移动;鼠标指针似乎以大约2:1的比例移动到拖动的UIView的移动。

第二个问题是,当拖动结束时,如果该项目没有被删除,我需要将UIView返回到它的原始比例,然后再将其重新附加到它的超级视图,并且还没有完全想到如何做到这一点。

感谢任何帮助,包括有关更好方法的建议,如果我完全偏离此处。 (我知道还有其他的东西需要在隔离放置目标和放弃时完成,但我想我知道那里需要做什么)。

感谢您的任何指导。

正则表达式

回答

0

似乎touchesmoved仍在使用旧的大小。作为一个建议,我将首先开始调整大小,然后实施拖放操作。

当拖放工作正在执行调整大小。首先开始拖动然后拖放。这样做可以让你更好地感受你想要达到的目标。

关于2:1的比例,我有一种感觉,它是关于你没有调整视图总体的变换,但我可能是错的。但请确保您使拖动视图更小,中间指向拖动点。

一些参考资料,可以帮助有:

http://www.edumobile.org/iphone/iphone-programming-tutorials/simple-drag-and-drop-on-iphone/ http://bynomial.com/blog/?p=77

+0

好的,我明白了,我想我会发布代码,以防万一别人需要它。我在这里发现了另一篇文章,其中有类似的问题: – RegularExpression 2013-02-20 17:50:06

2

好,我知道了答案&想我应该张贴在这里的情况下,任何人都需要它。感谢以前的答复。我碰到下面的文章:

How to move a UIImageView after applying CGAffineTransformRotate to it?

,这给我带来了以下解决方案。在我的原始代码中,我将变换应用到开始位置,但我没有将它应用到触摸点。所以,这里是我结束了与解决的问题:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self.superview bringSubviewToFront:self]; 
    self.transform = CGAffineTransformMakeScale(0.2f, 0.2f); 
    startLocation = ([[touches anyObject] locationInView:self]); 
    startLocation = CGPointApplyAffineTransform(startLocation, self.transform); 
} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGPoint pt = [[touches anyObject] locationInView:self]; 
    if (!CGAffineTransformIsIdentity(self.transform)) 
     pt = CGPointApplyAffineTransform(pt, self.transform); 
    float dx = pt.x - startLocation.x; 
    float dy = pt.y - startLocation.y; 
    CGPoint newCenter = CGPointMake(self.center.x + dx, self.center.y + dy); 
    self.center = newCenter; 
} 

因此,需要变换当且仅当变换已应用到UIView的被应用到触摸点。我一度将转换应用于接触点,但没有条件 - 在这种情况下,整个拖动操作的起点很差,因为转换已应用于触点,可能在点之前已被移动。无论如何,上面的代码似乎已经解决了这个问题。

+0

此外,新中心点不需要进行转换。 – RegularExpression 2013-02-20 18:05:26