Created
June 11, 2014 10:52
-
-
Save luigisaggese/5b63dd6eb34a9088ca4b to your computer and use it in GitHub Desktop.
PulsingHaloLayer for pierceboggan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using MonoTouch.Foundation; | |
using MonoTouch.UIKit; | |
using MonoTouch.CoreAnimation; | |
using System.Drawing; | |
using System.Threading.Tasks; | |
namespace PulsingHaloLayer | |
{ | |
public class PulsingHaloLayer : CALayer | |
{ | |
CAAnimationGroup animationGroup; | |
double animationDuration; | |
double pulseInterval; | |
float radius; | |
public PulsingHaloLayer () | |
{ | |
this.ContentsScale = UIScreen.MainScreen.Scale; | |
this.Opacity = 0; | |
// default | |
this.radius = 60; | |
this.animationDuration = 3; | |
this.pulseInterval = 0; | |
this.BackgroundColor = UIColor.FromRGBA (0, 0.478f, 1, 1).CGColor; | |
Task.Factory.StartNew(Dispatcher); | |
//await DispatchAsync(); | |
/* | |
if(this.pulseInterval != INFINITY) { | |
dispatch_async(dispatch_get_main_queue(), ^(void) { | |
[this addAnimation:this.animationGroup forKey:@" pulse"]; | |
}); | |
} | |
}); | |
*/ | |
} | |
async void Dispatcher() | |
{ | |
await DispatchAsync (); | |
} | |
public async Task setRadius(float raggio, UIColor color){ | |
this.BackgroundColor = color.CGColor;//UIColor.FromRGBA (0, 0.487f, 1.0f, 1.0f).CGColor; | |
radius = raggio; | |
PointF tempPos = this.Position; | |
float diameter = this.radius * 2; | |
this.Bounds = new RectangleF(0, 0, diameter, diameter); | |
this.CornerRadius = this.radius; | |
this.Position = tempPos; | |
} | |
public async Task DispatchAsync() | |
{ | |
this.animationGroup = CAAnimationGroup.CreateAnimation (); | |
this.animationGroup.Duration = this.animationDuration + this.pulseInterval; | |
this.animationGroup.RepeatCount = float.MaxValue; | |
this.animationGroup.RemovedOnCompletion = false; | |
this.animationGroup.TimingFunction = CAMediaTimingFunction.FromName (CAMediaTimingFunction.Linear); | |
var scaleAnimation = new CABasicAnimation { | |
From = NSNumber.FromFloat (0), | |
To = NSNumber.FromFloat (50), | |
KeyPath = "transform.scale.xy", | |
Duration = this.animationDuration | |
}; | |
var opacityAnimation = (CAKeyFrameAnimation)CAKeyFrameAnimation.FromKeyPath ("opacity"); | |
opacityAnimation.Duration = this.animationDuration; | |
opacityAnimation.Values = new NSNumber[]{ | |
NSNumber.FromFloat (0.45f), | |
NSNumber.FromFloat (0.45f), | |
0 | |
}; | |
/* | |
opacityAnimation.KeyTimes = new NSNumber[]{ | |
0, | |
NSNumber.FromFloat (0.2f), | |
1 | |
}; | |
*/ | |
opacityAnimation.RemovedOnCompletion = false; | |
this.animationGroup.Animations = new CAAnimation [] { scaleAnimation, opacityAnimation}; | |
if (pulseInterval != double.MaxValue) { | |
await aggiungiAnimazione (); | |
} | |
} | |
public async Task aggiungiAnimazione(){ | |
this.AddAnimation (animationGroup, "pulse"); | |
} | |
} | |
} | |
______________________ | |
HOW TO USE | |
public override void ViewDidLoad() | |
{ | |
base.ViewDidLoad(); | |
PulsingHaloLayer halo = new PulsingHaloLayer (); | |
UIButton btnricerca = new UIButton (UIButtonType.Custom); | |
btnricerca.Frame = new RectangleF(120,390,80,80); | |
halo.Position = btnricerca.Center; | |
this.View.Layer.InsertSublayerBelow (halo, btnricerca.Layer); | |
btnricerca.BackgroundColor = UIColor.White; | |
// Put on a rounded border | |
btnricerca.Layer.BorderWidth = 2; | |
btnricerca.Layer.BorderColor = new MonoTouch.CoreGraphics.CGColor(0,0,0,0.9f); | |
btnricerca.Layer.CornerRadius =40; | |
halo.setRadius(0.5f * 3, UIColor.Yellow); | |
// Setup the title text color | |
btnricerca.SetTitleColor(UIColor.DarkGray, UIControlState.Normal); | |
View.AddSubview (btnricerca); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment