Ios 包含UIImageView的UITableView和UITableViewCell
好的,所以我只能在UIImageView的边界外滚动,但我需要能够在这些视图的顶部滚动。UIImageView包含平移手势、挤压手势,我不确定这是否是导致错误的原因。我将在下面粘贴我的手机代码:Ios 包含UIImageView的UITableView和UITableViewCell,ios,iphone,swift,uitableview,uiimageview,Ios,Iphone,Swift,Uitableview,Uiimageview,好的,所以我只能在UIImageView的边界外滚动,但我需要能够在这些视图的顶部滚动。UIImageView包含平移手势、挤压手势,我不确定这是否是导致错误的原因。我将在下面粘贴我的手机代码: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { imageIndex = indexPath.row var cel
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
imageIndex = indexPath.row
var cell : XpandDraggableCell = tableView.dequeueReusableCellWithIdentifier("XpandDraggableCell", forIndexPath: indexPath) as! XpandDraggableCell
cell.imgview.backgroundColor = UIColor(red: 210, green: 211, blue: 213, alpha: 1.0)
if (horizontal) {
defaultX = defaultSpacingX * imageIndex; /* E.g 70 * 2 = 140 from origin X. */
}else{
defaultY = defaultSpacingY * imageIndex; /* E.g 70 * 2 = 140 from origin Y. */
}
cell.imgview.frame = CGRect(x: cell.imgview.frame.origin.x, y: cell.imgview.frame.origin.y, width: 75.0, height: 75.0)
var cellWidth : CGFloat = cell.contentView.frame.width
var cellHeight : CGFloat = cell.contentView.frame.height
cell.imgview.center = CGPoint(x: cell.contentView.frame.origin.x + (cellWidth / 2), y: cell.contentView.frame.origin.y + (cellHeight / 2))
cell.imgview.image = UIImage(named: images[imageIndex]) /*Image provided*/
cell.imgview.contentMode = UIViewContentMode.ScaleAspectFit
cell.imgview.userInteractionEnabled = true
cell.imgview.tag = imageIndex
var addToViewPanGesture : UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector("imageViewPanned:"))
cell.imgview.addGestureRecognizer(addToViewPanGesture)
var doubleTapAddGesture : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("cellDoubleTapped:"))
doubleTapAddGesture.numberOfTapsRequired = 2
cell.imgview.addGestureRecognizer(doubleTapAddGesture)
cell.imgview.backgroundColor = UIColor.clearColor()
cell.imgview.layer.backgroundColor = UIColor.clearColor().CGColor
cell.imgviewbtn.backgroundColor = UIColor.clearColor()
cell.backgroundColor = UIColor(red: 210, green: 211, blue: 213, alpha: 1.0)
return cell
}
class XpandDraggableCell : UITableViewCell{
@IBOutlet var imgviewbtn: UIButton!
@IBOutlet var imgview: UIImageView!
}
平移和滚动是“相同”的手势 问题在于imageView的平移手势识别器识别并处理手势(而不是失败或将触摸传递到cell/tableView) 如果希望能够向任何方向平移图像,可以在tableViewCell上设置平移手势识别器,而不是图像 然后检查手势的位置是否在imageView中。如果是这样,开始这个手势,这样平移手势就会识别它。当您在单元格外“平移”时,平移手势不会识别该手势,而是可以识别为滚动
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint point = [gestureRecognizer locationInView:self.view];
CGRect frame = self.imageView.frame;
if (CGRectContainsPoint(frame, point))
{
return YES;
}
else
{
return NO;
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *gestureView = [gestureRecognizer view];
CGPoint translation = [gestureRecognizer translationInView:[gestureView superview]];
// Check for horizontal gesture
if (fabsf(translation.x) > fabsf(translation.y))
{
return YES;
}
return NO;
}
更新:
根据您编辑的问题,您需要做的是让手势识别器区分拖动和滚动。同样,这些都是类似的手势
如果您触摸图像,然后移动手指,iOS如何知道您是要拖动图像,还是要滚动表格?您必须做出有根据的猜测:
- 如果手势向左,则可能是拖动。平底锅手势 识别器应该识别这个手势
- 如果手势向上或向下,则可能是滚动。平底锅手势 识别器应使手势失败
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint point = [gestureRecognizer locationInView:self.view];
CGRect frame = self.imageView.frame;
if (CGRectContainsPoint(frame, point))
{
return YES;
}
else
{
return NO;
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *gestureView = [gestureRecognizer view];
CGPoint translation = [gestureRecognizer translationInView:[gestureView superview]];
// Check for horizontal gesture
if (fabsf(translation.x) > fabsf(translation.y))
{
return YES;
}
return NO;
}
这将阻止平移手势识别器将垂直手势识别为平移,并且它们应该根据您的需要滚动表格
您将在中找到更多信息
话虽如此,其他开发人员通过使用长按手势启动拖动,绕过了平移或滚动tableView问题。在这种情况下,很明显,任何平移手势都将用于启动的拖动,否则任何手势都将滚动表格(或滑动单元格)
您的应用程序可能不需要滑动来删除,但通常最好不要干扰、冲突或更改用户已经知道的预期手势的行为。好的,因此,这是否允许我在单元格中滚动图像视图,而不必滚动到uiimageview的边界之外?为了清楚起见,我需要设置单元格的平移手势,而不是现在的图像视图?是的,是的。这将允许您在触摸位于图像视图内时平移图像视图(您的手势识别器),或在触摸位于图像视图外时滚动表格(内置手势)。好的,这不起作用。也许我没有正确编写代码,但这是我视图的一个图像,其中包含UITableView、UITableViewCell->UIImageView。我需要能够像正常一样在图像顶部滚动,但也能够像正常一样平移它们。看起来整个图像都是可见的。图像有什么可平移的?基本上它是一个拖放功能。您可以将图像从右侧的容器拖动到左侧,将其放置在我创建的EnvironmentImageView的顶部,并能够创建您选择的场景。现在,这是关于拖放的。根据您最初的提问,您只提到了tableview单元格和图像,您的问题听起来像是在单元格内平移。我将创建一个新问题,显示您的全屏,并解释您是从表外拖动到表内,还是从表内拖动到表外,还是从表内拖动到表外。:)它是从右侧的表视图到左侧的环境图像视图。你有办法让我私下联系你吗?你现在似乎明白我的意思了,如果你愿意的话,我可以开始另一个问题。我在对我的答案的编辑中回答了这个问题。我现在没有时间写代码,但是如果你仍然卡住了,我会尝试稍后帮助你。