Рисование пальцем в приложении Xamarin.iOS (C #)

2

Я делаю приложение, где пользователь может что-то нарисовать пальцами... Я понятия не имею, как я могу это сделать...

После исследования я нашел код ниже.

Но с помощью этого кода я могу сделать только одну строку. Если я закончил касание и новое событие касания начнет линию, продолжайте в этом пункте... Старая линия автоматически подключается к новой.

Поэтому я хочу создать новую линию на каждом касании.

Кто-нибудь знает, как это работает?

код

    CGPath path;
    CGPoint initialPoint;
    CGPoint latestPoint;

    public DrawView (IntPtr handle) : base (handle)
    {
        BackgroundColor = UIColor.White;

        path = new CGPath();
    }

    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
        base.TouchesBegan(touches, evt);

        UITouch touch = touches.AnyObject as UITouch;

        if (touch != null)
        {
            initialPoint = touch.LocationInView(this);
        }
    }

    public override void TouchesMoved(NSSet touches, UIEvent evt)
    {
        base.TouchesMoved(touches, evt);

        UITouch touch = touches.AnyObject as UITouch;

        if (touch != null)
        {
            latestPoint = touch.LocationInView(this);
            SetNeedsDisplay();
        }
    }

    public override void Draw(CGRect rect)
    {
        base.Draw(rect);

        if (!initialPoint.IsEmpty)
        {

            //get graphics context
            using (CGContext g = UIGraphics.GetCurrentContext())
            {
                //set up drawing attributes
                g.SetLineWidth(2);
                UIColor.Black.SetStroke();

                //add lines to the touch points
                if (path.IsEmpty)
                {
                    path.AddLines(new CGPoint[] { initialPoint, latestPoint });
                }
                else
                {
                    path.AddLineToPoint(latestPoint);
                }

                //add geometry to graphics context and draw it
                g.AddPath(path);

                g.DrawPath(CGPathDrawingMode.Stroke);
            }
        }
    }
Теги:
xamarin.ios
drawing
uiview

1 ответ

1
Лучший ответ

Вы можете создать дополнительный CGPath для записи путей и нарисовать их с помощью CGContext

Код:

public partial class DrawLine : UIView
{

    CGPath pathtotal;
    CGPath path;

    CGPoint initialPoint;
    CGPoint latestPoint;
    public DrawLine(IntPtr handle) : base(handle)
    {
        BackgroundColor = UIColor.White;
        pathtotal = new CGPath();
    }
    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
        base.TouchesBegan(touches, evt);
        path = new CGPath();
        UITouch touch = touches.AnyObject as UITouch;

        if (touch != null)
        {
            initialPoint = touch.LocationInView(this);
        }
    }

    public override void TouchesMoved(NSSet touches, UIEvent evt)
    {
        base.TouchesMoved(touches, evt);

        UITouch touch = touches.AnyObject as UITouch;

        if (touch != null)
        {
            latestPoint = touch.LocationInView(this);
            SetNeedsDisplay();
        }
    }

    public override void Draw(CGRect rect)
    {
        base.Draw(rect);

        if (!initialPoint.IsEmpty)
        {

            //get graphics context
            using (CGContext g = UIGraphics.GetCurrentContext())
            {
                //set up drawing attributes
                g.SetLineWidth(2);
                UIColor.Black.SetStroke();

                //add lines to the touch points
                if (path.IsEmpty)
                {
                    path.AddLines(new CGPoint[] { initialPoint, latestPoint });
                }
                else
                {
                    path.AddLineToPoint(latestPoint);
                }

                //add geometry to graphics context and draw it
                pathtotal.AddPath(path);

                g.AddPath(pathtotal);
                g.DrawPath(CGPathDrawingMode.Stroke);
            }
        }
    }
}

Результат испытаний:

Изображение 174551

  • 0
    Большое спасибо ... Это прекрасно работает
  • 0
    Как будет выглядеть код, если вы хотите рисовать в классе UIImage?

Ещё вопросы

Сообщество Overcoder
Наверх
Меню