objective-c - 苹果手机 : responder chain in UIScrollView

标签 objective-c ios

如何将触摸事件传递到我的 View Controller ?我认为 UIScrollView 正在拦截触摸并导致我在 View Controller 中的触摸事件不触发

代码片段:

    //
//  DragDrop.m
//  Ballet
//
//  Created by  on 1/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DragDrop.h"
#import "BAScrollView.h"

@implementation DragDrop

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

        [self.view setBackgroundColor:[UIColor whiteColor]];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    sv = [[BAScrollView alloc] initWithFrame:self.view.frame];


    [sv setBackgroundColor:[UIColor redColor]];

    UIImageView *dragImage2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img-thumb-10-mindful.png"]];

    [dragImage2 setUserInteractionEnabled:YES];

    dragImage2.frame = CGRectMake(0, 0, 64, 64);

    [sv addSubview:dragImage2];


    [self.view addSubview:sv];

    //this image works as expected
    UIImageView *dragImage3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img-thumb-10-mindful.png"]];

    [dragImage3 setUserInteractionEnabled:YES];

    dragImage3.frame = CGRectMake(200, 200, 64, 64);

    [self.view addSubview:dragImage3];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    if ([touch.view isKindOfClass:[UIImageView class]])
    {
        dragImage = [[UIImageView alloc] initWithImage:((UIImageView *)(touch.view)).image];
        dragImage.frame = CGRectMake(100, 100, 64, 64);//touch.view.frame;
        [dragImage setUserInteractionEnabled:YES];
      //  CGPoint point = [[[event allTouches] anyObject] locationInView:super.view];

        //dragImage.frame = 
       // dragImage.center = point;
        [self.view addSubview:dragImage];



    }



}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    NSLog(@"MOVED");

    if (dragImage!=nil) {
        CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];

        dragImage.center = point;
    }





}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (dragImage != nil)
    {

        [dragImage removeFromSuperview];
        [dragImage release];
        dragImage = nil;
    }
}


@end
  • SCROLLVIEW 子类

// //BAScrollView.m //芭蕾 // //由 n 1/10/12 创建。 //版权所有 (c) 2012 我的公司名称。版权所有。 //

#import <Foundation/Foundation.h>
#import "BAScrollView.h"

@implementation BAScrollView

- (id)initWithFrame:(CGRect)frame 
{
    return [super initWithFrame:frame];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesBegan:touches withEvent:event]; 


}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesMoved:touches withEvent:event]; 
}

- (void)touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
{   
    // If not dragging, send event to next responder
    if (!self.dragging) 
        [self.nextResponder touchesEnded: touches withEvent:event]; 
    else
        [super touchesEnded: touches withEvent: event];
}



@end

最佳答案

a good article about Responder Chain by Jeff Lamarche

在您的情况下,您可以子类化您的 ScrollView 并将您的触摸事件委托(delegate)给响应者链中的下一个。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if(condition) // you want scroll to happen
        [super touchesBegan:touches withEvent:event];
    else // you want to delegate your touch to the next responder
        [self.nextResponder touchesBegan:touches withEvent:event];
}

这不是经过测试的代码,但我希望你明白这一点。

关于objective-c - 苹果手机 : responder chain in UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8814413/

相关文章:

ios - 在哪里突出显示 UICollectionViewCell : delegate or cell?

iOS:如果已安装,则使用自定义 URL 打开应用程序,否则使用 iTunes

ios - SWIFT 中的 KVO 测试

ios - C 数组作为 iOS 中的属性

ios - 我怎样才能让罗盘针头朝南?

iphone - 使用保留 iOS 刷新 TableView 时内存泄漏

ios - 从几张图片中剖析ImageView

ios - 无法在 detailTextLabel 中显示 NSString

ios - 我的 iOS 委托(delegate)方法是否应该始终在主线程上返回?

iphone - 执行多线程的正确方法