jjzjj

ios - 设备旋转通知未按预期 iOS Swift 工作

coder 2023-09-14 原文

    func shouldirotate(){

    var whichwaywhere: String {

        if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft {
            return "left"
        }
        if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight {
            return "right"
        }
        if UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown {
            return "down"
        }
        return "I don't Care"
    }
    println(whichwaywhere)
}

当我创建一个持续检查自身的函数时,(将 shouldirotate 附加到 NSTimer)我可以检查方向。我如何告诉函数在 UIDeviceOrientationDidChangeNotification 激活时运行?

为了让 UIDeviceOrientationDidChangeNotification 运行,我有一个变量:

var rotatenote: Bool = UIDevice.currentDevice().generatesDeviceOrientationNotifications

在 ViewDidLoad 覆盖中我有:

override func viewDidLoad() {
    UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
    rotatenote = true
}

这是使用和声明此属性的正确方法吗?我怎样才能让函数运行 DeviceOrientationNotification?

P.S.:我在 ViewDidLoad 中有 rotatenote = true 因为如果我像往常一样尝试将它附加到变量声明,它会说“无法分配给这个表达式的结果。”见下文

var rotatenote: Bool = UIDevice.currentDevice().generatesDeviceOrientationNotifications = true

最佳答案

有几种方法可以做到这一点。不确定您担心的操作系统是什么,但在 iOS 8 中您有两个简单的选择。您可以注册观察员

UIDeviceOrientationDidChangeNotification

或者你可以覆盖

func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator 协调器: UIViewControllerTransitionCoordinator)

你不应该需要

UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()

所以要么在 viewDidLoad 添加:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "functionThatYouWantTriggeredOnRotation", name:
        UIDeviceOrientationDidChangeNotification, object: nil)

你有一个函数 functionThatYouWantTriggeredOnRotation 来进行计算...

或者,您可以只为 viewWillTransition 提供覆盖,而不必为 UIDeviceOrientationDidChangeNotification 添加观察者

override func viewWillTransitionToSize(size: CGSize,
      withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
    if UIDevice.currentDevice().orientation.isLandscape {
            //do your thing
        }
}

关于ios - 设备旋转通知未按预期 iOS Swift 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28786754/

有关ios - 设备旋转通知未按预期 iOS Swift 工作的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. 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

  3. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  4. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  5. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  6. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  7. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  8. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  9. ruby - JetBrains RubyMine 3.2.4 调试器不工作 - 2

    使用Ruby1.9.2运行IDE提示说需要gemruby​​-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall

  10. ruby-on-rails - ruby 日期方程不返回预期的真值 - 2

    为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998

随机推荐