簡體   English   中英

iphone:UIScrollView中的響應鏈

[英]iphone: responder chain in UIScrollView

如何將觸摸事件傳遞給我的viewcontroller? 我認為UIScrollView正在觸及觸摸並導致視圖控制器中的觸摸事件不會觸發

代碼段:

    //
//  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 SUB-CLASS

// // BAScrollView.m // Ballet // //由n 1/10/12創建。 //版權所有(c)2012 MyCompanyName 版權所有。 //

#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

Jeff Lamarche一篇關於響應者鏈的好文章

在您的情況下,您可以子類化滾動視圖並將觸摸事件委托給響應者鏈中的下一個事件。

- (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];
}

這不是經過測試的代碼,但我希望你能得到這個想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM