草庐IT

ios - 如何设置 UIScrollView 的子类来响应触摸事件?

coder 2024-01-12 原文

我最近遇到了无法从 UIScrollView 中注册触摸事件(touchesMoved 等)的问题。在与很多人交谈并阅读了大量帖子后,事实证明 UIScrollView 本身不接受触摸,而是需要将触摸传递给 UIScrollView 子类。

我是 objective-c 的新手,我花了很长时间思考如何定义子类(在单独的 .h/.m 文件中或以某种方式在 @interface 或其他东西中),以及 iOS 如何处理与响应链。

请让我描述一下我的具体情况,任何愿意用菜鸟语言解释我需要做什么的人都将不胜感激。

我的应用程序中只有一个页面,我将它的默认 UIView 更改为 UIScrollView,所以如果我在 InterfaceBuidler 中检查它我看到的是我的 SampleViewControllerUIScrollView。我将 UIScrollView 链接到 SampleViewControler.h@Interface,如下所示:

#import <UIKit/UIKit.h>
@interface SampleViewController : UIViewController
{
}
@property (strong, nonatomic) IBOutlet UIScrollView *mainScroller;
@end

我在 SampleViewController.m 中引用了我的 UIScrollView,如下所示:

#import "SampleViewController.h"
@interface SampleViewController ()
@end

@implementation SampleViewController
@synthesize mainScroller = _mainScroller;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Movement!");
}

-(void)viewWillAppear:(BOOL)animated
{
    [_mainScroller setContentOffset:CGPointMake(0,0) animated:YES];
}

如果我将 UIScrollView 的类更改为 UIView,则会检测到触摸并触发一些 NSLog 消息。但是,一旦 UIView 设置回 UIScrollView,触摸将被忽略。

据我了解,我需要使用以下代码设置 UIScrollView 的子类:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Movement!");
}

...然后更改我的 SampleViewController.m 以获得代码:

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

但是,我真的不明白 1) 如何设置这样的子类,以及 2) 为什么这会起作用,因为我不明白为什么 [[self.nextResponder nextResponder] touchesMoved:touches withEvent: event]; 将首先从 SampleViewController.m 中调用,因为我类似的 NSLog(@"Movement!"); 语句永远不会执行。

我已经尝试了我能找到的所有教程,并且正在转向 SE 的专业知识!谢谢。

最佳答案

你想错了。如果您想知道 scrollView 何时移动,则无需对其进行子类化。 iOS 已在 UIScrollViewDelegate 中设置了您需要的所有方法。但是,如果您想说,为基于 scrollView 的项目选择 touchEvents,那完全没问题。然后,您只需要对 scrollView 中的项目进行子类化,然后在各自的类中选择 touchEvents。但是对于标准的 scrollView 方法,你应该这样设置:

.H

  #import <UIKit/UIKit.h>
  @interface SampleViewController : UIViewController<UIScrollViewDelegate>
  {
  }
  @property (strong, nonatomic) IBOutlet UIScrollView *mainScroller;
  @end

.M

#import "SampleViewController.h"

@interface SampleViewController ()
@end

@implementation SampleViewController
@synthesize mainScroller = _mainScroller;

-(void)viewDidLoad
{
   self.mainScroller.delegate=self;//Adopt the delegate for the scrollView..this is the important line
}

然后只需实现 UIScrollViewDelegateMethods 来抓取移动等。当试图在 scrollView/tableView 中拾取 touchEvents 时会发生未定义的行为,因为 scrollView 本身已经在幕后实现了这些 touchEvents

查看开发者网站上的所有方法并在您的 .M 文件中实现它们。由于您在 viewDidLoad 中采用了委托(delegate),因此这些将是您的 scrollView 回调。我之前已经列出了其中的一些,但请继续查看 UIScrollViewDelegate Protocol site

– scrollViewDidScroll:
– scrollViewWillBeginDragging:
– scrollViewWillEndDragging:withVelocity:targetContentOffset:
– scrollViewDidEndDragging:willDecelerate:
– scrollViewShouldScrollToTop:
– scrollViewDidScrollToTop:
– scrollViewWillBeginDecelerating:
– scrollViewDidEndDecelerating:

关于ios - 如何设置 UIScrollView 的子类来响应触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17077242/

有关ios - 如何设置 UIScrollView 的子类来响应触摸事件?的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  4. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  5. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  6. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  7. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  8. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  9. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  10. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

随机推荐