【问题标题】:How do I implement a draggable UIView subclass within a UIScrollView?如何在 UIScrollView 中实现可拖动的 UIView 子类?
【发布时间】:2009-10-26 13:20:03
【问题描述】:

我有一个相当棘手的滚动场景需要排序。我有一个包含两个子 UIView 子类的单个容器视图的父 UIScrollView。称它们为 viewA 和 viewB。 UIScrollView 已被限制为水平滚动/缩放。 ViewA 和 ViewB 被布置为水平条,一个在另一个之上。 viewA 和 viewB 的边界超出了 scrollView 的边界,并且在滚动/缩放期间正常显示。到目前为止一切顺利。

这里有点棘手:我需要能够垂直和水平拖动 viewB。我不想将 viewB 嵌套在它自己的滚动视图中。

我的问题是,什么是为 viewB 实现简单垂直滚动的正确方法,同时仍然让它在 scrollView 方面表现正确?我是否简单地实现了四种触摸序列方法:在 viewB 中开始/移动/结束/取消,操作 viewB 的变换矩阵,我很清楚还是更多?

提前致谢。

干杯, 道格

【问题讨论】:

  • 为什么不希望 ViewB 出现在它自己的滚动视图中?
  • 一句话:复杂性。 scrollView 对我的需求来说太过分了。

标签: cocoa-touch uiview uiscrollview


【解决方案1】:

好吧,关于您给定的场景,我将按以下方式解决此问题:

首先创建一个 UIView-Subclass,包括必要的功能,这个是接收你的拖动动作。虽然这是可拖动的,但您应该禁用 UIScrollView 的滚动。 标题:

#import <UIKit/UIKit.h>;

@interface ViewB : UIView
{
}

@end

实施:

#import "ViewB.h"

@implementation DragView 
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.alpha = 0.5;
    ((UIScrollView*)self.superview.superview).scrollEnabled = NO;
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint loc = [touch locationInView:self.superview];
    self.center = loc;
}

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.alpha = 1.0;
    ((UIScrollView*)self.superview.superview).scrollEnabled = YES;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.alpha = 1.0;
    ((UIScrollView*)self.superview.superview).scrollEnabled = YES;
}

// Further methods...

@end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    相关资源
    最近更新 更多