我有一个底部视图(UIView)和一个顶部视图(UIView)。bottomView 具有红色背景,在 IB 中,我将 topView 设置为 clearColor 并将 Opaque 设置为 OFF。使用以下代码,我实现了在 topView 上创建一个矩形/窗口并在 bottomView 上打一个洞的目标:
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGRect holeRect = CGRectMake(100, 100, 100, 100);
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
// Start by filling the area with the blue color
[[UIColor blueColor] setFill];
UIRectFill( rect );
// Assume that there's an ivar somewhere called holeRect of type CGRect
// We could just fill holeRect, but it's more efficient to only fill the
// area we're being asked to draw.
CGRect holeRectIntersection = CGRectIntersection( holeRect, rect );
[[UIColor clearColor] setFill];
UIRectFill( holeRectIntersection );
}
现在 - 我想让矩形/窗口可移动。我已将 ViewController 设置为 UIGestureRecognizerDelegate 并在实现文件中,在 viewDidLoad 中有以下代码在另一个项目中对我有用:
// 为每个 ImageView 创建平移手势识别器 UIPanGestureRecognizer *pan1Recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan1From:)];
// set self as delegate for gesture recognition
[pan1Recognizer setDelegate:self];
// setup the selector for the pan gesture
pan1Recognizer.minimumNumberOfTouches = 1;
pan1Recognizer.maximumNumberOfTouches = 1;
[XXX addGestureRecognizer:pan1Recognizer];
// ensure user interaction is ENABLED !!!!
[XXX setUserInteractionEnabled:YES];
挑战是,上面的XXX。在我的另一个项目中,我在 IB 中有一个子视图,其中定义了一个出口,因此我可以明确引用它。但是在这个项目中,我在上面的 drawRect 中创建了矩形,但不知道如何建立这种连接。
任何帮助表示赞赏!