コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
using Xamarin.Forms;
namespace AppName.Controls
{
[System.Diagnostics.DebuggerStepThrough()]
public class BaseLabel : Label
{
public BaseLabel() : base()
{
}
public BaseLabel(bool useAnimation) : base()
{
this.UseAnimation = useAnimation;
}
public static BindableProperty UseAnimationProperty = BindableProperty.Create(
propertyName: "UseAnimation",
returnType: typeof(bool),
declaringType: typeof(BaseLabel),
defaultValue: true,
defaultBindingMode: BindingMode.TwoWay);
public bool UseAnimation
{
get
{
return (bool)GetValue(UseAnimationProperty); ;
}
set
{
SetValue(UseAnimationProperty, value);
}
}
}
}
using System;
using System.ComponentModel;
using CoreAnimation;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using AppName.iOS.Renderer;
using AppName.Controls;
[assembly: ExportRenderer(typeof(BaseLabel), typeof(CustomLabelRenderer))]
namespace AppName.iOS.Renderer
{
public class CustomLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Element == null)
{
return;
}
try
{
var view = (BaseLabel)Element;
if (e.PropertyName == BaseLabel.TextProperty.PropertyName &&
view.UseAnimation)
{
//アニメーション設定
CATransition anim = CATransition.CreateAnimation();
anim.Type = CATransition.TransitionMoveIn;
anim.Subtype = CATransition.TransitionFromBottom;
anim.Duration = 0.2f;
Control.Layer.AddAnimation(anim, null);
//テキストを設定
Control.Text = view.Text;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + System.Environment.NewLine +
ex.StackTrace);
}
}
}
}
using System;
using System.ComponentModel;
using Android.Views.Animations;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using AppName.Droid.Renderer;
using AppName.Controls;
[assembly: ExportRenderer(typeof(BaseLabel), typeof(CustomLabelRenderer))]
namespace AppName.Droid.Renderer
{
public class CustomLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
try
{
if (Element == null)
{
return;
}
var view = (BaseLabel)Element;
if (e.PropertyName == BaseLabel.TextProperty.PropertyName &&
view.UseAnimation)
{
//アニメーション設定
Control.Alpha = 0f; //透過に設定
TranslateAnimation anim = new TranslateAnimation(0, 0, -Control.Height, 0);
anim.Duration = 200;
anim.RepeatCount = 0;
Control.StartAnimation(anim);
Control.Animate().Alpha(1f);
Control.Animate().SetDuration(500);
Control.Animate().SetListener(null);
//テキストを設定
Control.Text = view.Text;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + System.Environment.NewLine +
ex.StackTrace);
}
}
}
}
using AppName.Services;
using Xamarin.Forms;
public class TestPage : ContentPage
{
private BaseLabel lblAnimationOn = new BaseLabel(true);
private BaseLabel lblAnimationOff = new BaseLabel(false);
void OnButtonClick()
{
for (int i = 0; i <= 100; i++)
{
this.lblAnimationOn.Text = "";
this.lblAnimationOn.Text = i.ToString();
this.lblAnimationOff.Text = i.ToString();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:base="clr-namespace:AppName.Controls;assembly=AppName"
x:Class="AppName.Views.TestPage">
<ContentPage.Content>
<base:BaseLabel x:Name="lblAnimationOn" Text="" UseAnimation="true"/>
<base:BaseLabel x:Name="lblAnimationOff" Text="" UseAnimation="false"/>
</ContentPage.Content>
</ContentPage>
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。