ios5 - 在 iOS 5 中子类化 UIScrollView 时无法拖动对象,有帮助吗?

标签 ios5 uiscrollview subclass drag ios4

首先,这个问题在iOS 4.3中并不存在。我仍然可以在 iOS 4.3 中运行该程序,并且不会发现任何问题。其次,这是一个 iPad 应用程序。

问题是,当我按住图像并拖动它时,它不会被拖动。它在 iOS 4.3 中有效,但在 iOS 5 中无效。

我创建了新的测试项目并清除了所有不需要的内容。我选择的项目是单 View 应用程序,没有更改任何 AppDelegate 文件。

这是代码。

myUIScrollViewClass.h

#import <UIKit/UIKit.h>


@interface myUIScrollViewClass : UIScrollView
{
}

@end

myUIScrollViewClass.m

#import "myUIScrollViewClass.h"


@implementation myUIScrollViewClass


- (id)init
{
    self = [super init];

    if (self) 
     {
     }

    return self;
}

- (void)didReceiveMemoryWarning
{
    [self didReceiveMemoryWarning];
}


-(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

{
     [self.nextResponder touchesEnded: touches withEvent:event]; 
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{
     [self.nextResponder touchesCancelled: touches withEvent:event]; 
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "myUIScrollViewClass.h"

@interface ViewController : UIViewController <UIScrollViewDelegate>
{
     myUIScrollViewClass *mainScrollView_;
     UIImageView *aTouchedImage_;
}

@property (retain, nonatomic) myUIScrollViewClass *mainScrollView_;
@property (retain, nonatomic) UIImageView *aTouchedImage_;

@end

ViewController.m

#import "ViewController.h"

@implementation ViewController

@synthesize mainScrollView_;
@synthesize aTouchedImage_;

- (void)didReceiveMemoryWarning
{
     [super didReceiveMemoryWarning];
     // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
     mainScrollView_ = [[myUIScrollViewClass alloc] initWithFrame:self.view.frame];
     [self.view addSubview:mainScrollView_];

     mainScrollView_.delegate = self;
     mainScrollView_.contentSize = CGSizeMake(1024, 768);
     mainScrollView_.clipsToBounds = YES;
     mainScrollView_.bounces = NO;
     mainScrollView_.bouncesZoom = NO;
     mainScrollView_.showsVerticalScrollIndicator = NO;
     mainScrollView_.showsHorizontalScrollIndicator = NO;
     mainScrollView_.backgroundColor = [UIColor whiteColor];
     mainScrollView_.userInteractionEnabled = YES;

     aTouchedImage_ = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"image1.png"]];
     [aTouchedImage_ setFrame:CGRectMake(0, 0, 80, 80)];
     [mainScrollView_ addSubview:aTouchedImage_];
     [aTouchedImage_ setCenter:CGPointMake(512, 334)];


     [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
}

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

- (void)viewWillAppear:(BOOL)animated
{     
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
     [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
     [super viewDidDisappear:animated];
}

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

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)anEvent
{
     mainScrollView_.scrollEnabled = NO;


     for (UITouch *touch in touches) 
     {
          [aTouchedImage_ setCenter:[touch locationInView:self.view]];
     }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)anEvent
{           
     mainScrollView_.scrollEnabled = NO;


     for (UITouch *touch in touches) 
     {
          [aTouchedImage_ setCenter:[touch locationInView:self.view]];
     }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)anEvent
{
     mainScrollView_.scrollEnabled = YES;


     for (UITouch *touch in touches)
     {        
     }
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)anEvent
{
    mainScrollView_.scrollEnabled = YES;

    for (UITouch *touch in touches) 
    {
    }
}

@end

最佳答案

看来我要回答我自己的问题了。

我已请求 Apple 开发人员技术支持提供支持,他们告诉我将代码放在 UIScrollView 中,而不是将触摸事件传递到另一个 View 。苹果自己也承认传递触摸事件存在问题。

以下是他们回复的摘录:

“看起来并非所有触摸事件都会到达您的 View Controller 。您的 UIScrollView 正在阻止触摸事件通过,即使它将这些事件转发给下一个响应者。行为非常不稳定,一些触摸事件是被转发到您的 View Controller ,但不是全部。”

“看起来 iOS 5.0.x 中的行为发生了变化。我猜这是因为引入了 View Controller 包含功能。”

另一种方法是使用 UIGestureRecognizer 来实现 View 的拖动。类似于下面的代码:

- (void)viewDidLoad 
{
    // ... other initialization code here

    UILongPressGestureRecognizer *longPress = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dragChild:)] autorelease];
    [longPress setMinimumPressDuration:0]; // recognize immediately when a finger comes down
    [draggableView addGestureRecognizer:longPress];
}

- (void)dragChild:(UILongPressGestureRecognizer *)longPress 
{
    [[longPress view] setCenter:[longPress locationInView:[[longPress view] superview]];
}

关于ios5 - 在 iOS 5 中子类化 UIScrollView 时无法拖动对象,有帮助吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9347157/

相关文章:

ios - 下载的文件应该放在哪个目录

iphone - UIScrollView 由于标签过多而导致性能问题

ios5 - 如何暂停、停止和重置 AUFilePlayer?

objective-c - iOS 中的 AES256 NSString 加密

objective-c - 我用选择器和目标调用dispatch_async。选择器被调用,但直到我单击主页按钮,界面才会更新

uiscrollview - 如何以编程方式滚动 SingleChildScrollView?

ios - 以编程方式在 UIScrollView 中使用 UIStackView

c++ - 如何检查一个类是否继承了另一个类?

swift - 以编程方式创建 SKScene 子类,没有大小信息?

Java,如何让上层类的变量保持为null;如何从子类中获取 'hide'变量?