Some examples to give a user a start point for Core Animation framework.
The ASObjC script could be run in Script Editor to show you the animation of NSView.
use framework "Foundation"
use framework "AppKit"
use scripting additions
property arguments : missing value
on run
try
if my NSThread's isMainThread() as boolean then
my performDialog:arguments
else
my performSelectorOnMainThread:"performDialog:" withObject:"arguments" waitUntilDone:true
end if
on error the errorMessage number the errorNumber
set the ErrorText to "Error: " & the errorNumber & ". " & the errorMessage
return the ErrorText
end try
end run
on performDialog:arguments
set theAnimation1 to createAnimation("position.x", 30, 420, 5, 1, true, 0)
set theAnimation2 to createAnimation("transform.scale.x", 1, 1.5, 5, 1, true, 0)
set theAnimation3 to createAnimation("transform.rotation.z", 30, 75, 5, 1, true, 0)
set theAnimation4 to createAnimationKeyframes("position.y", {0, 10, -30, 10, 0}, {0, 0.25, 0.5, 0.75, 1}, 5, true)
set theRectangle to createView(40, 60, 30, 30)
theRectangle's setBackgroundColor:(current application's NSColor's systemRedColor())
theRectangle's setWantsLayer:true
theRectangle's layer()'s addAnimation:theAnimation1 forKey:"animationPosition"
theRectangle's layer()'s addAnimation:theAnimation2 forKey:"animationTransformScale"
theRectangle's layer()'s addAnimation:theAnimation3 forKey:"animationTransformRotation"
theRectangle's layer()'s addAnimation:theAnimation4 forKey:"animationPositionKeyframe"
set theBox to createBoxWithRect("Core Animation Example", "futura", 20, 20, 560, 110)
set subviewItems to {theRectangle, theBox}
set theWindow to createWindowWithRectAndSubview(subviewItems, 0, 0, 600, 140)
theWindow's |center|()
theWindow's makeKeyAndOrderFront:me
end performDialog:
(**
* [createAnimation(keyPath, fromValue, toValue, duration, speed, reverseBool, repeatCount)]
* Reference:https://developer.apple.com/documentation/quartzcore/cabasicanimation?language=objc
*)
on createAnimation(keyPath, fromValue, toValue, duration, speed, reverseBool, repeatCount)
set theAnimation to current application's CABasicAnimation's animationWithKeyPath:keyPath
-- Defines the value the receiver uses to start interpolation.
theAnimation's setFromValue:fromValue
-- Defines the value the receiver uses to end interpolation.
theAnimation's setToValue:toValue
-- Specifies the basic duration of the animation, in seconds.
theAnimation's setDuration:duration
-- Specifies how time is mapped to receiver’s time space from the parent time space. Default: 1.0
theAnimation's setSpeed:speed
-- Determines if the receiver plays in the reverse upon completion. Default: false
theAnimation's setAutoreverses:reverseBool
-- Determines the number of times the animation will repeat. Default: 0
theAnimation's setRepeatCount:repeatCount
return theAnimation
end createAnimation
(**
* []
*)
on createAnimationKeyframes(keyPath, theValues, keyTimes, duration, isAdditiveBool)
set theAnimation to current application's CAKeyframeAnimation's animationWithKeyPath:keyPath
theAnimation's setValues:theValues
theAnimation's setKeyTimes:keyTimes
theAnimation's setDuration:duration
theAnimation's setAdditive:isAdditiveBool
return theAnimation
end createAnimationKeyframes
on createView(x, y, width, height)
set viewRectSize to current application's NSMakeRect(x, y, width, height)
set theView to current application's NSView's alloc()'s initWithFrame:viewRectSize
return theView
end createView
on createBoxWithRect(title, |font|, x, y, width, height)
set boxSize to current application's NSMakeRect(x, y, width, height)
set theBox to current application's NSBox's alloc()'s initWithFrame:boxSize
theBox's setTitle:title
theBox's setTitleFont:(current application's NSFont's fontWithName:|font| |size|:12)
-- Specify the location of a box’s title with respect to its border.
theBox's setTitlePosition:(current application's NSAtTop)
-- theBox's setTransparent:false
-- theBox's setBoxType:(current application's NSBoxCustom)
-- theBox's setBorderType:(current application's NSLineBorder)
theBox's setBorderWidth:0.5
theBox's setCornerRadius:5
-- theBox's setFillColor:(current application's NSColor's lightBlueColor)
theBox's setBorderColor:(current application's NSColor's orangeColor)
return theBox
end createBoxWithRect
on createWindowWithRectAndSubview(subviewItems, x, y, width, height)
set windowSize to current application's NSMakeRect(x, y, width, height)
set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer) + (current application's NSWindowStyleMaskMiniaturizable as integer) + (current application's NSWindowStyleMaskResizable as integer)
set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:true
repeat with anSubview in subviewItems
(theWindow's contentView()'s addSubview:anSubview)
end repeat
return theWindow
end createWindowWithRectAndSubview