自定义“可滑动”UIScrollView在iPad上无法正常工作 - 在iPhone上运行正常

时间:2010-08-28 22:24:05

标签: iphone ipad uiscrollview

我正在努力使我的iPhone应用程序与iPad兼容。用户可以点击或滑动屏幕以激活某些功能,它可以在我的iPhone版本上正常工作。页面上有一个UIScrollView,我将其子类化为“可刷卡”,即它将所有触摸功能传递给它的超级视图:

 @implementation SwipeableScrollView

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

    [self.superview touchesBegan:touches withEvent:event];

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

    [self.superview touchesMoved:touches withEvent:event];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];

    [self.superview touchesEnded:touches withEvent:event];
} 

@end

这在iPhone版本上运行良好,通过了点击和滑动手势,但在iPad上,我得到以下奇怪的行为:

  1. 水龙头正确传递给superview。
  2. 但是,根本没有通过滑动手势。
  3. 知道为什么这会在iPhone而不是iPad上工作吗?

3 个答案:

答案 0 :(得分:0)

虽然在iPhone上需要覆盖触摸事件,但对于iPad,Apple已经引入了UIGestureRecognizers,使得这样的任务变得更加简单易行。您可能需要重构代码才能使用它们。

答案 1 :(得分:0)

问题是,即使canCancelContentTouches设置为NO,iPad上的UIScrollView也会非常快速地取消内容触摸。此外,覆盖-touchesShouldCancelInContentView:也无济于事。在此处阅读更多内容:link text

答案 2 :(得分:0)

你可以自定义uiscrollView并实现它的委托,这样你就可以用滚动视图做你想做的事情吗?

 #import <UIKit/UIKit.h>
         @protocol ScrollingDelegate <NSObject>
         @required
        - (void)scrolltouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
        - (void)scrolltouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
        - (void)scrolltouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
         @end

实施

 @interface CustomScrollView : UIScrollView
 {
  id <ScrollingDelegate> delegate;

  }
  @property (nonatomic,strong) id scrollDelegate;
  @end

   #import "CustomScrollView.h"

   @implementation CustomScrollView
   @synthesize scrollDelegate;
   - (id)initWithFrame:(CGRect)frame
   {
  self = [super initWithFrame:frame];
  if (self) {
    // Initialization code
   }
   return self;
   }

  - (id)initWithCoder:(NSCoder *)aDecoder
  {
  self=[super initWithCoder:aDecoder];
  if (self)
  {
  }
  return self;
  }
  -(BOOL)touchesShouldCancelInContentView:(UIView *)view
  {
   return NO;
  }

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

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

   - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    [self.scrollDelegate scrolltouchesEnded:touches withEvent:event];

    }

    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
    }
    @end