Dailycode.info

Short solution for short problems

iOS: Make a simple iOS7 ready button by subclassing UIButton

Very simple solution, but works like a charm.

Create a subclass of UIButton. 

There we overload the initFrame and important also the awakeFromNib:

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        [GeneralFunctionsMakeNormalButton:self];

    }

    returnself;

}

 

-(void) awakeFromNib

{

    [GeneralFunctionsMakeNormalButton:self];

 

}

 
This general function looks like this:

+ (void) MakeNormalButton:(UIView *) vw

{

    vw.backgroundColor= [UIColorwhiteColor];

    [vw.layersetCornerRadius:10.0f];

    [vw.layersetBorderColor:[UIColorblueColor].CGColor];

    [vw.layersetBorderWidth:1.0f];

    [vw.layersetShadowColor:[UIColorgrayColor].CGColor];

    [vw.layersetShadowOpacity:0.5];

    [vw.layersetShadowRadius:2.0];

    [vw.layersetShadowOffset:CGSizeMake(2.0f, 2.0f)];

}

Now we also overwrite the setHighlighted method to show the button was selected:

-(void) setHighlighted:(BOOL)highlighted

{

    if (highlighted)

    {

        [selfhighLightView];

    }

    else

    {

        [selfcleanHighLightView];

    }

    [super setHighlighted:highlighted];

 

}

 

-(void) highLightView

{

    self.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);

    self.layer.shadowOpacity = 0.25;

    self.backgroundColor= [UIColorblueColor];

}

 

-(void) cleanHighLightView

{

    self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);

    self.layer.shadowOpacity = 0.5;

    self.backgroundColor= [UIColorwhiteColor];

}

 

Now you can change the class of your button in the storyboard designer to this class and your buttons get the new layout, no more programming todo.

 

Then from button less iOS 7 and button richt iOS 6 you get a uniform layout with very few changes. In the examples below, you'll see the iOS 6 layout on top, iOS 7 on the bottom.

 

 


No validation with the "Cancel" button

I was having a hard time with some forms that had control validation occuring. Especially when the "Cancel" button was pressed, the validation still occured. I was looking for a property on the button called autovalidate or something, but I looked over the CausesValidation property. If you set this to false, the validation will be ignored! Thank god! Found this here: http://stackoverflow.com/questions/1882523/how-to-skip-validating-after-clicking-on-a-forms-cancel-button

 


Start and stop animations in WPF from code behind

It sounds simple, and it is.

I followed the simple Microsoft demonstration which you can find here.

I had a start and stop button and defined a trigger on the button to start the animation when it was clicked. Still was working fine. Then I decided to use the same button to start and stop the animation and this is where I got stuck for a short while.

The trigger on the XAML defined that each time the click was called, the event should be triggered. But when I stopped the animation in the onclick, the animation was still running. So this means the trigger is executed after the onclick event is handled. To solve this I removed the trigger and placed the storyboard with the animations in the resources:

<Window.Resources>

        <Storyboard x:Key="ButtonStoryBoard">

            <DoubleAnimation Name="btnStartdoubleAnimation"

                                        Storyboard.TargetName="btnStart"

                                        Storyboard.TargetProperty="Opacity"

                                        From="1.0" To="0.0" Duration="0:0:5"

                                        AutoReverse="True" RepeatBehavior="Forever" />

            <ColorAnimation Storyboard.TargetName="MyColorBrush" 

                                        Storyboard.TargetProperty="Color"

                                        From="Green"

                                        To="Yellow"              

                                        Duration="0:0:10"

                                        AutoReverse="True" RepeatBehavior="Forever"/>

            <ColorAnimation Storyboard.TargetName="MyBorderColorBrush" 

                                        Storyboard.TargetProperty="Color"

                                        From="DeepSkyBlue"

                                        To="AntiqueWhite"              

                                        Duration="0:0:15"

                                        AutoReverse="True" RepeatBehavior="Forever"/>

 

        </Storyboard>

    </Window.Resources>

Then I removed the trigger in XAML and simply started and stopped the storyboard animations in code.

It looks like this:

Storyboard storyboard;

 

        private void btnStart_Click(object sender, RoutedEventArgs e)

        {

            storyboard = this.TryFindResource("ButtonStoryBoard") as Storyboard;

 

            if (!_started)

            {

               

                if (storyboard != null)

                {

                    storyboard.Begin(btnStart, true);

 

                }

 

                _started = true;

                btnStart.Content="Stop";

            }

            else

            {

                _started = false;

 

                if (storyboard != null)

                {

                    storyboard.Stop(btnStart);

                }

 

 

                btnStart.Content = "Start";

            }

        }

So the animations are started and stopped from the code behind.

Found most of the info here.