Xamarin.iOS启动图片过渡及广告展示


Xamarin.iOS启动图片过渡及广告展示

Xamarin.iOS


1.实现效果

启动应用时,启动图片自然过渡到广告图片中(外加基本动画【波纹及上翻页】),如:
20141028172751673.gif-1682kB

2.实现方式

在FinishedLaunching执行时,往UIWindow里AddSubview视图,在LaunchingView视图中通过模拟添加启动图片来实现自然过渡,从而达到显示广告的效果。

3.代码实现

1)控件实例

  1. public class LaunchingView:UIView
  2. {
  3. private UIActivityIndicatorView actView;
  4. private UIImageView defauleView;
  5. private UIImage imgDefault;
  6. private UIImage webImage;
  7. private Action<bool> actLoaded;
  8. private NSTimer cutDownTimer; // 接口加载计时
  9. private bool IsLoaded; //
  10. private const float LoadingTime = 7;
  11. private double changeLauchTime = 5;
  12. public LaunchingView (UIWindow _window,Action<bool> _actLoaded)
  13. {
  14. this.actLoaded = _actLoaded;
  15. this.Frame = _window.Frame;
  16. if (_window.Frame.Height > 480) {
  17. imgDefault = UIImage.FromFile("Images/Default-568h.png");
  18. } else {
  19. imgDefault = UIImage.FromFile("Images/Default.png");
  20. }
  21. defauleView = new UIImageView (_window.Frame);
  22. defauleView.Image = imgDefault;
  23. this.AddSubviews (defauleView);
  24. actView = new UIActivityIndicatorView (UIActivityIndicatorViewStyle.Gray);
  25. actView.Frame = new RectangleF ((this.Frame.Width-30)/2,(this.Frame.Height-30)/2,30,30);
  26. this.AddSubview (actView);
  27. this.CutDownTime ();
  28. }

2)计时器,控制图片加载时间

  1. #region 接口计时
  2. public void CutDownTime()
  3. {
  4. this.LoadADViewData ();
  5. //实时接口超时处理,自动跳转
  6. int count = 0;
  7. cutDownTimer = NSTimer.CreateRepeatingScheduledTimer (1,()=>{
  8. count++;
  9. if(IsLoaded){
  10. this.Invalidate();
  11. return;
  12. }
  13. if(count == LoadingTime){
  14. this.Invalidate();
  15. this.TheAnimation();
  16. }
  17. });
  18. }
  19. private void Invalidate()
  20. {
  21. if(cutDownTimer != null){
  22. cutDownTimer.Invalidate();
  23. cutDownTimer.Dispose();
  24. }
  25. }
  26. #endregion

3)图片加载处理

  1. #region 启动图片处理
  2. private void LoadADViewData()
  3. {
  4. var imageUrl = "http://f.hiphotos.baidu.com/image/pic/item/9358d109b3de9c82bc5dfb826f81800a19d84386.jpg";
  5. this.ShowTimeSetting (imageUrl);
  6. }
  7. private void ShowTimeSetting(string imageUrl)
  8. {
  9. if(!string.IsNullOrEmpty(imageUrl)){
  10. actView.StartAnimating ();
  11. var picUrl = NSUrl.FromString(imageUrl);
  12. var manager = SDWebImageManager.SharedManager;
  13. manager.Download (picUrl,SDWebImageOptions.ProgressiveDownload,ProgessHandler,CompletedHandler);
  14. }
  15. }
  16. void ProgessHandler(uint cuSize,long total)
  17. {
  18. //下载图片进度
  19. }
  20. void CompletedHandler (UIImage image, NSError error, SDImageCacheType cacheType,bool finshed)
  21. {
  22. //下载完成
  23. this.Invalidate();
  24. actView.StopAnimating ();
  25. this.IsLoaded = true;
  26. if(image != null){
  27. webImage = image;
  28. }
  29. this.TheAnimation ();
  30. }
  31. #endregion

4)动画过渡控制

  1. #region 动画过渡
  2. private void TheAnimation ()
  3. {
  4. if (webImage == null) {
  5. //当没有节日图片时,设置画面停留时间短暂
  6. changeLauchTime = 0;
  7. }
  8. CATransition animation = CATransition.CreateAnimation ();
  9. animation.Duration = 1.5f;
  10. animation.TimingFunction = CAMediaTimingFunction.FromName (CAMediaTimingFunction.EaseInEaseOut);
  11. animation.Type = "rippleEffect";
  12. if (webImage != null) {
  13. this.defauleView.Image = webImage;
  14. }
  15. this.Layer.AddAnimation (animation, "animation");
  16. PerformSelector (new Selector ("ToUpSide"), null, changeLauchTime);
  17. }
  18. [Export ("ToUpSide")]
  19. private void ToUpSide ()
  20. {
  21. //翻页动画
  22. this.actLoaded (true);
  23. UIView.BeginAnimations ("ChangeView");
  24. UIView.SetAnimationDuration (1.0f);
  25. UIView.SetAnimationCurve (UIViewAnimationCurve.EaseInOut);
  26. UIView.SetAnimationTransition (UIViewAnimationTransition.CurlUp, this, true);
  27. UIView.SetAnimationDelegate (this);
  28. UIView.SetAnimationWillStartSelector (new Selector("WillStart"));
  29. UIView.SetAnimationDidStopSelector (new Selector("DidMove"));
  30. UIView.CommitAnimations ();
  31. }
  32. [Export("DidMove")]
  33. private void DidMovew()
  34. {
  35. this.RemoveFromSuperview ();
  36. }
  37. [Export("WillStart")]
  38. private void WillStart()
  39. {
  40. defauleView.RemoveFromSuperview ();
  41. }
  42. #endregion

5)调用

  1. public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
  2. {
  3. // window = new UIWindow (UIScreen.MainScreen.Bounds);
  4. // window.RootViewController = new RootViewController();
  5. //// window.
  6. var launchView = new LaunchingView (Window, (result) => {
  7. });
  8. Window.MakeKeyAndVisible ();
  9. Window.AddSubview (launchView);
  10. // launchView.CutDownTime();
  11. return true;
  12. }

作者:zhaowensky_126
原文地址:http://blog.csdn.net/zhaowensky_126/article/details/40542853

分享到