gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:08:27 25 4
gpt4 key购买 nike

如何将触摸事件传递到我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com