慢慢初始化导致缺乏用户体验的组件

时间:2012-04-09 18:20:58

标签: xcode uiview uiviewcontroller xamarin.ios

这是我单击上一个视图上的按钮时显示的视图。 文本框,笑脸图像和标签是由xCode创建的预先设计的。

请查看视图的图像和代码,以清除为什么所有视图的组件初始化非常缓慢,并准备好在完成完全加载后拍摄的最后一次拍摄。此外,当我输入字母时非常慢,当我使用iOS在文本框上的每次触摸时提供的键盘输入时,字母显得非常缓慢。

enter image description here

观点代码;

using System;
using System.Drawing;

using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace IstanbulCity
{
    public partial class AskForNAme : UIViewController
    {
        public delegate void AskForNAmeClosingDelegate (AskForNAme form);

        public event AskForNAmeClosingDelegate AskForNAmeClosed;
        NSObject obs1;
        float scrollamount = 0.0f;
        float bottomPoint = 0.0f;
        float yOffset = 0.2f;
        bool moveViewUp = false;

        public AskForNAme () : base ("AskForNAme", null)
        {


        }

        public override void DidReceiveMemoryWarning ()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning ();

            // Release any cached data, images, etc that aren't in use.
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();


            // Perform any additional setup after loading the view, typically from a nib.
        }
        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(true);
                obs1 = NSNotificationCenter.DefaultCenter.AddObserver (
"UIKeyboardDidShowNotification", KeyboardUpNotification);
            this.tbOwnerMailAdress.ShouldReturn += TextFieldShouldReturn;
            this.tbOwnerBirthDay.ShouldReturn += TextFieldShouldReturn;
            this.uivGuguPhoto.Image = UIImage.FromFile ("image/fcuk.jpeg");

        }
        public override void ViewWillAppear(bool animated)
        {  
            base.ViewWillAppear(false);
            obs1 = NSNotificationCenter.DefaultCenter.AddObserver (
"UIKeyboardDidShowNotification", KeyboardUpNotification);
            this.tbOwnerMailAdress.ShouldReturn += TextFieldShouldReturn;
            this.tbOwnerBirthDay.ShouldReturn += TextFieldShouldReturn;
            this.uivGuguPhoto.Image = UIImage.FromFile ("image/fcuk.jpeg");

        }
        public override void ViewDidUnload ()
        {
            base.ViewDidUnload ();

            // Clear any references to subviews of the main view in order to
            // allow the Garbage Collector to collect them sooner.
            //
            // e.g. myOutlet.Dispose (); myOutlet = null;

            ReleaseDesignerOutlets ();
        }

        public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
        {
            // Return true for supported orientations
            return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
        }

        void HandleIstanbulCityViewControllerClosed (babyAge form)
        {
            form.DismissModalViewControllerAnimated (true);
            form = null;
        }



        partial void tbKadikoyHallEditDidEndOnExit (MonoTouch.Foundation.NSObject sender)
        {
            tbIstanbulName.ResignFirstResponder ();
        }



        private bool TextFieldShouldReturn (UITextField tf)
        {
            tf.ResignFirstResponder ();
            if (moveViewUp) {
                ScrollTheView (false);
            }
            return true;
        }

        private void KeyboardUpNotification (NSNotification notification)
        {
            ResetTheView ();

            RectangleF r = UIKeyboard.BoundsFromNotification (notification);

            if (this.tbOwnerMailAdress.IsEditing ) {
                //Calculate the bottom of the Texbox
                //plus a small margin...
                bottomPoint = (this.tbOwnerMailAdress.Frame.Y + this.tbOwnerMailAdress.Frame.Height + yOffset);

                //Calculate the amount to scroll the view
                //upwards so the Textbox becomes visible...
                //This is the height of the Keyboard -
                //(the height of the display - the bottom
                //of the Texbox)... 
                scrollamount = (r.Height - (View.Frame.Size.Height - bottomPoint));
            }
            else if (this.tbOwnerBirthDay.IsEditing)
            {
                bottomPoint = (this.tbOwnerBirthDay.Frame.Y + this.tbOwnerBirthDay.Frame.Height + yOffset);
                scrollamount = (r.Height - (View.Frame.Size.Height - bottomPoint));
            }
            else
            {
                scrollamount = 0;
            }

            //Check to see whether the view
            //should be moved up...
            if (scrollamount > 0) {
                moveViewUp = true;
                ScrollTheView (moveViewUp);
            }  else
                moveViewUp = false;
        }

        private void ResetTheView ()
        {
            UIView.BeginAnimations (string.Empty, System.IntPtr.Zero);
            UIView.SetAnimationDuration (0.3);

            RectangleF frame = View.Frame;
            frame.Y = 0;
            View.Frame = frame;
            UIView.CommitAnimations ();
        }

        private void ScrollTheView (bool movedUp)
        {
//To invoke a views built-in animation behaviour,
//you create an animation block and
//set the duration of the move...
//Set the display scroll animation and duration...
            UIView.BeginAnimations (string.Empty, System.IntPtr.Zero);
            UIView.SetAnimationDuration (0.3);

//Get Display size...
            RectangleF frame = View.Frame;

            if (movedUp) {
//If the view should be moved up,
//subtract the keyboard height from the display...
                frame.Y -= scrollamount;
            }  else {
//If the view shouldn't be moved up, restore it
//by adding the keyboard height back to the original...
                frame.Y += scrollamount;
            }

//Assign the new frame to the view...
            View.Frame = frame;

//Tell the view that your all done with setting
//the animation parameters, and it should
//start the animation...
            UIView.CommitAnimations ();

        }
    }
}

最新版本 - 仍然是相同的用户体验'慢!

using System;
using System.Drawing;

using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace IstanbulCity
{
    public partial class AskForNAme : UIViewController
    {
        public delegate void AskForNAmeClosingDelegate (AskForNAme form);

        public event AskForNAmeClosingDelegate AskForNAmeClosed;


        public AskForNAme () : base ("AskForNAme", null)
        {


        }

        public override void DidReceiveMemoryWarning ()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning ();

            // Release any cached data, images, etc that aren't in use.
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();


            // Perform any additional setup after loading the view, typically from a nib.
        }


        public override void ViewDidUnload ()
        {
            base.ViewDidUnload ();

            // Clear any references to subviews of the main view in order to
            // allow the Garbage Collector to collect them sooner.
            //
            // e.g. myOutlet.Dispose (); myOutlet = null;

            ReleaseDesignerOutlets ();
        }

        public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
        {
            // Return true for supported orientations
            return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
        }

        void HandleIstanbulCityViewControllerClosed (babyAge form)
        {
            form.DismissModalViewControllerAnimated (true);
            form = null;
        }







    }
}

1 个答案:

答案 0 :(得分:2)

这看起来与初始化无关。您正在添加ViewDidAppearViewWillAppear的通知。您还总是在每个键盘通知上调用ResetTheView(动画)(即使没有其他更改)。

我的猜测是你经常以ResetTheView方式调用 - 并且连续的动画会破坏你的应用程序的性能。

您可以通过在Console.WriteLine方法中添加ResetTheView和可能的计数器来确认这一点。