Sha256: 1582af9d88e7b20f877b90e134ce0ba10180a4a004e73c1a6156f9ac4820e58c

Contents?: true

Size: 1.19 KB

Versions: 18

Compression:

Stored size: 1.19 KB

Contents

module MotionKit
  module_function

  # performs a breadth-first search
  def find_first_view(root, &condition)
    if condition.call(root)
      root
    else
      found = nil
      root.subviews.find do |subview|
        found = find_first_view(subview, &condition)
      end
      return found
    end
  end

  # performs a depth-first and reversed search
  def find_last_view(root, &condition)
    found = nil
    root.subviews.reverse.find do |subview|
      found = find_last_view(subview, &condition)
    end

    if ! found && condition.call(root)
      found = root
    end

    return found
  end

  # performs a breadth-first search, but returns all matches
  def find_all_views(root, &condition)
    found = []
    if condition.call(root)
      found << root
    end

    (root.subviews || []).each do |subview|
      found.concat(find_all_views(subview, &condition))
    end

    return found
  end

  # similar to find_all_views, but for CALayer
  def find_all_layers(root, &condition)
    found = []
    if condition.call(root)
      found << root
    end

    (root.sublayers || []).each do |sublayer|
      found.concat(find_all_layers(sublayer, &condition))
    end

    return found
  end

end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
motion-kit-0.10.11 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.10.10 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.10.9 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.10.8 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.10.7 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.10.6 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.10.5 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.10.4 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.10.3 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.10.2 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.10.1 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.10.0 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.9.6 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.9.4 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.9.3 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.9.2 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.9.1 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.9.0 lib/motion-kit-cocoa/cocoa_util.rb