Skip to content

Instantly share code, notes, and snippets.

@praeclarum
Created December 20, 2014 19:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save praeclarum/3074dc240b138a2a9c32 to your computer and use it in GitHub Desktop.
Save praeclarum/3074dc240b138a2a9c32 to your computer and use it in GitHub Desktop.
A little iOS app that shows a noisy square moving in a field of noise
namespace NoiseAnimation
#nowarn "64"
open System
open UIKit
open Foundation
open CoreGraphics
[<AutoOpen>]
module Conversions =
///Converts a primitive type that support op_Implicit into the inferred type
let inline implicit< ^a,^b when ^a : (static member op_Implicit : ^b -> ^a)> arg =
(^a : (static member op_Implicit : ^b -> ^a) arg)
///Converts a primitive type that support op_Implicit into nint
let inline nint (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> nint) x)
///Converts a primitive type that support op_Implicit into nfloat
let inline nfloat (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> nfloat) x)
///Converts a primitive type that support op_Implicit into nuint
let inline nuint (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> nuint) x)
type NoiseView () =
inherit UIView ()
override this.Draw r =
let rand = new Random (123)
let c = UIGraphics.GetCurrentContext ()
let f = this.Bounds
let right = f.Right
let bottom = f.Bottom
let d : nfloat = (implicit 1.0f) / this.Window.Screen.Scale
let mutable x = f.X
while x < right do
let mutable y = f.Y
while y < bottom do
if rand.Next (0, 2) = 0 then
c.SetFillColor (implicit 0, implicit 1)
else
c.SetFillColor (implicit 1, implicit 1)
c.FillRect (CGRect (x, y, d, d))
y <- y + d
x <- x + d
type MyViewController () =
inherit UIViewController ()
let random = new Random ()
let rand () = float32 (Math.Round ((random.NextDouble () - 0.5) * 200.0))
override this.ViewDidLoad () =
let nv = new NoiseView (Frame = this.View.Bounds)
let v = new NoiseView (BackgroundColor = UIColor.Red, Frame = CGRect (implicit 100, implicit 200, implicit 100, implicit 100))
this.View.Add (nv)
this.View.Add (v)
let doTap (tap : UITapGestureRecognizer) =
let doAnimate () =
let mutable f = v.Frame
f.X <- f.X + nfloat (rand ())
f.Y <- f.Y + nfloat (rand ())
v.Frame <- f
UIView.Animate (2.0, Action doAnimate)
let g = new UITapGestureRecognizer (doTap)
this.View.AddGestureRecognizer (g)
[<Register("AppDelegate")>]
type AppDelegate() =
inherit UIApplicationDelegate()
let mutable win : UIWindow = null
override this.Window with get () = win
// This method is invoked when the application is ready to run.
override this.FinishedLaunching(app, options) =
win <- new UIWindow(UIScreen.MainScreen.Bounds)
// If you have defined a root view controller, set it here:
this.Window.RootViewController <- new MyViewController ()
this.Window.MakeKeyAndVisible()
true
module Main =
[<EntryPoint>]
let main args =
UIApplication.Main(args, null, "AppDelegate")
0
@praeclarum
Copy link
Author

Here is a demo of it working:

Video of noisy animation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment