gpt4 book ai didi

objective-c - 在点和手指之间画一条恒定线

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:41 25 4
gpt4 key购买 nike

我想做与应用程序 Scalar 所做的类似的事情,他们提供了从一个点拖动到记事本上以将数字粘贴到他们也拖动点的位置的功能。我真正感兴趣的是与点和手指保持连接的线,如下所示:

enter image description here

我的问题是我真的不知道这叫什么,所以我在寻找如何做到这一点时遇到了麻烦。有谁知道这会叫什么,他们有关于这个主题的任何教程吗?如果您有一些代码供我查看,那就更好了。

谢谢。

最佳答案

这个 UIView 示例会在手指在其上拖动时绘制线条并检测要触摸的第一个 View ,应该可以帮助您入门。

//this goes in the header file called "UILineView.h"

#import <UIKit/UIKit.h>

@interface UILineView : UIView

@end

//this in the implementation file called "UILineView.m"

#import "UILineView.h"

@implementation UILineView

{
CGPoint _originOfTouchPoint; // your fist touch detected in touchesBegan: method
CGPoint _currentFingerPositionPoint; // the position you have dragged your finger to
CGFloat _strokeWidth; // the width of the line you wish to draw
id _touchStartedObject; // the object(UIView) that the first touch was detected on
}

// If you use Interface Builder to design your interface, Objects in a nib file are reconstituted and then initialized using
// their initWithCoder: method
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self) {
// Initialization code
_originOfTouchPoint = CGPointMake( 0.0, 0.0 );
_currentFingerPositionPoint = CGPointMake( 100.0, 100.0 );
_strokeWidth = 2.0;
}
return self;
}
/*
// Use initWithFrame if you are not loding the UIView from a nib file
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_originOfTouchPoint = CGPointMake( 0.0, 0.0 );
_currentFingerPositionPoint = CGPointMake( 100.0, 100.0 );
_strokeWidth = 2.0;
}
return self;
}
*/

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor( context, [UIColor blueColor].CGColor );
CGContextSetLineWidth( context, _strokeWidth );
// fisrt point of line
CGContextMoveToPoint( context, _originOfTouchPoint.x, _originOfTouchPoint.y );
// last point of line
CGContextAddLineToPoint( context, _currentFingerPositionPoint.x, _currentFingerPositionPoint.y );
// draw the line
CGContextStrokePath( context );
}

#pragma mark touches

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// get starting point and first view touched (if you need to send that view messages)
_originOfTouchPoint = [[touches anyObject] locationInView:self];
_touchStartedObject = [[touches anyObject] view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint movedToPoint = [[touches anyObject] locationInView:self];
// if moved to a new point redraw the line
if ( CGPointEqualToPoint( movedToPoint, _currentFingerPositionPoint ) == NO )
{
_currentFingerPositionPoint = movedToPoint;
// calls drawRect: method to show updated line
[self setNeedsDisplay];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// reset values
_originOfTouchPoint = CGPointZero;
_currentFingerPositionPoint = CGPointZero;
_touchStartedObject = nil;
}

@end

关于objective-c - 在点和手指之间画一条恒定线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11845754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com