2012-02-19 4 views
1

まず、この問題はiOS 4.3では存在しませんでした。私はまだiOS 4.3でプログラムを実行することができ、何の問題も見られません。第二に、これはiPadアプリです。iOS 5でUIScrollViewをサブクラス化するときにオブジェクトをドラッグできません。

問題は、画像を押したままドラッグすると、ドラッグされないということです。 iOS 4.3では動作しましたが、iOS 5では動作しませんでした。

私は新しいテストプロジェクトを作成し、不要なものはすべてクリアしました。私が選択したプロジェクトはシングルビューアプリケーションで、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 

答えて

1

私は自分の質問に答えるようです。

アップルデベロッパーテクニカルサポートからのサポートを依頼しており、タッチイベントを別のビューに渡すのではなく、自分のコードをUIScrollViewに配置するように指示しました。 Appleはタッチイベントを渡すことに問題があると認めています。ここで

は、その回答の抽出物である:

「ではないすべてのタッチイベントは、あなたのビューコントローラに至るまで取得しているように見えますあなたのUIScrollViewのはへ転送し、それらのイベントであっても、通じなってからタッチイベントをブロックしています。次のレスポンダー。この動作は非常に不安定で、一部のタッチイベントはView Controllerに転送されていますが、すべてではありません。

"iOS 5.0.xでの動作の変化のようです。これは、導入されたビューコントローラの格納機能のためです。"

これを行うもう1つの方法は、UIGestureRecognizerを使用してビューのドラッグを実装することです。次のコードのようなもの:

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

単純なUIViewの半分をカバーするスクロールビューでダミープロジェクトを試しました。 Scrollviewにスクロールが無効になっていました。 uiviewには、nslogsを呼び出して呼び出されているかどうかを確認するメソッドがありました。スクロールビューに触れると、touchesBegan、touchesEnded、touchesMovedが呼び出されていませんでした。 uiviewの部分に触れると、メソッドが呼び出されていました。これは、setScrollEnabledのドキュメントとは異なり、レスポンダチェーン上でイベントを転送しないUIScrollViewのバグを確認します。 – Jon

関連する問題