[英]Getting a UIImageView object to show up when dragging (with UILongPressGestureRecognizer)
因此,我正在嘗試對使用ALAssets Framework獲取的圖像縮略圖進行拖放。
我能夠長按手勢以使其正常工作(根據某些NSLog
測試,它肯定會四處移動),但我一生都無法找出如何實際顯示縮略圖並將其拖動到主屏幕中的方法。視圖。 請注意, _dNewImageView
是一個IBOutlet
作為屬性添加到視圖控制器。 我正在嘗試通過BringSubviewToFront將視圖放到最前面:但是它沒有按預期工作。 任何幫助,將不勝感激!
這是處理長按手勢的功能:
-(void)longGestureRecognized:(UILongPressGestureRecognizer *)gestureRecognizer{
gestureRecognizer.delaysTouchesBegan = YES;
CGPoint newPoint = [gestureRecognizer locationInView:self.collectionView];
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
dIndexPath = [self.collectionView indexPathForItemAtPoint:newPoint];
if (dIndexPath == nil){
NSLog(@"Couldn't find index path");
}
dCell = (SKPhotoCell *)[self.collectionView cellForItemAtIndexPath:dIndexPath];
dImage = [UIImage imageWithCGImage:[dCell.asset thumbnail]];
_dNewImageView = [[UIImageView alloc] initWithImage:dImage];
[_dNewImageView setUserInteractionEnabled:YES];
[[self view] bringSubviewToFront:_dNewImageView];
break;
case UIGestureRecognizerStateChanged:
[_dNewImageView setCenter:newPoint];
break;
case UIGestureRecognizerStateEnded:
break;
}
}
您需要添加兩件事:
[self.view insertSubview:_dNewImageView aboveSubview:self.collectionView];
無論您在何處分配和初始化_dNewImageView
。
和
[gestureRecognizer setTranslation:CGPointZero inView:gestureRecognizer.view];
在您的手勢識別方法中。
如果按照代碼建議的方式重用_dNewImageView
,則不應在每次調用該方法時進行分配和初始化。 使用[_dNewImageView setImage:dImage];
此外,當您檢查indexPath
存在時,請確保為該方法實現某種返回或繞過圖像內容的方法。
例如:
- (void) someOtherMethod {
_dNewImageView = [[UIImageView alloc] init];
_dNewImageView.frame = CGRectSomething;
_dNewImageView.userInteractionEnabled = YES;
[self.view insertSubview:_dNewImageView aboveSubview:self.collectionView];
}
- (void) longGestureRecognized:(UILongPressGestureRecognizer *)gestureRecognizer {
...
case UIGestureRecognizerStateBegan:
...
if (dIndexPath == nil) {
NSLog(...);
return; /*or*/ goto noIndexPath;
}
...
break;
//if used 'goto'
noIndexPath:
[gestureRecognizer setTranslation:CGPointZero inView:gestureRecognizer.view];
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.