- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想用手指在屏幕上画一个箭头。我的想法是通过触摸屏幕来设置箭头的初始坐标,当我在屏幕上拖动时,箭头会延伸并跟随我的手指。箭头的高度和宽度将相同,重要的是箭头的大小。当我将它拖离起点时,箭头会变长。我用这样的东西试过它:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UIGraphicsBeginImageContext(CGSizeMake(1536, 2048));
UITouch *touch = [touches anyObject];
CGPoint p1 = [touch locationInView:self.view];
CGSize size;
size.width = 50;
size.height = 400;
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawArrowWithContext:context atPoint:p1 withSize:size lineWidth:4 arrowHeight:20 andColor:[UIColor whiteColor]];
// converts your context into a UIImage
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// Adds that image into an imageView and sticks it on the screen.
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
}
和
- (void) drawArrowWithContext:(CGContextRef)context atPoint:(CGPoint)startPoint withSize: (CGSize)size lineWidth:(float)width arrowHeight:(float)aheight andColor:(UIColor *)color
{
float width_wing = (size.width - width) / 2;
float main = size.height-aheight;
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextSetStrokeColorWithColor(context, [color CGColor]);
CGPoint rectangle_points[] = {
CGPointMake(startPoint.x + width_wing, startPoint.y + 0.0),
CGPointMake(startPoint.x + width_wing, startPoint.y + main),
CGPointMake(startPoint.x + 0.0, startPoint.y + main), // left point
CGPointMake(startPoint.x + size.width / 2, startPoint.y + size.height),
CGPointMake(startPoint.x + size.width, startPoint.y + main), // right point
CGPointMake(startPoint.x + size.width-width_wing, startPoint.y + main),
CGPointMake(startPoint.x + size.width-width_wing, startPoint.y + 0.0),
CGPointMake(startPoint.x + width_wing, startPoint.y + 0.0),
};
CGContextAddLines(context, rectangle_points, 8);
CGContextFillPath(context);
}
如果我从普通 IBOutlet 中移动的触摸运行代码,箭头确实会出现在屏幕上,但这不是我的想法。我还没有设法让这段代码工作,但我认为即使它工作了,也会导致崩溃,因为我每次都删除并重新绘制形状。这是正确的方法吗?我应该怎么办?
最佳答案
基本上,有一些不同的选择。
第二个我将在这里简要描述:
检测事件的类需要一个成员变量/属性,ArrowView *arrowView 和一些可以记住起点的东西,可能是 CGPoint startPoint。ArrowView 需要箭头参数的属性,CGPoint arrowStart,CGSize arrowSize。在触摸事件处理程序中,执行
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint position = [touch locationInView:self.view];
[self.startPoint startFrom:position];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint next = [touch locationInView:self.view];
CGSize size;
size.width = next.x - self.startPoint.x;
size.height = next.y - self.startPoint.y;
self.arrowView.arrowPoint = self.startPoint;
self.arrowView.arrowSize = size;
[self.arrowView setNeedsDisplay];
}
在箭头 View 中:
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
// Now do the drawing stuff here using that context!
// self.arrowSize / self.arrowPoint do contain your drawing parameters.
...
}
我希望这能给你一个想法。
如需进一步阅读,start here.
关于ios - 使用 TouchesMoved 绘制形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14467111/
我正在尝试使用一个 ThumbStick 和两个按钮来实现用户控制节点。我创建了单独的 SKSpriteNode,其上带有控制元素,并覆盖父节点的触摸事件来处理用户触摸。 问题是,当我启动触摸屏时,即
我正在使用 Spritekit 创建一个适用于 iOS 7 的游戏。该游戏使用此代码在屏幕上移动圆圈 _enemy = [SKSpriteNode spriteNodeWithImageNamed:@
我试图允许用户在屏幕上拖动标签,但在模拟器中,每次我触摸屏幕上的某个位置时,它只会移动一点。它会跳到该位置,然后稍微拖动,但随后它将停止拖动,我必须触摸不同的位置才能使其再次移动。这是我的 .m 文件
我的 iOS 应用程序有一些问题。 我已经使用 clock() 函数检查了 touchesMoved 中的时间戳,并使用了当前值和先前值之间的差异。第一和第二事件之间的差异大于其他事件。 你有什么想法
我希望当用户从左向右滑动时,标签计数器从 1 更新到 5。如果我滑动得很慢,它会很慢地工作,然后再快一点,但它不工作,对吗?还有其他方法可以实现吗? 我有这个代码: -(void) touchesBe
我正在尝试通过单击并拖动元素来更新多个图像。我已经实现了 touchesbegan、touchesmoved 和 touchesended,但我不知道如何使 touchesmoved 影响多个图像。
我有一个位于表格 View 上方的半透明 View 。当我将我的 View 拖到屏幕底部附近时,我想平滑地滚动它下面的表格 View 。 我正在尝试模仿: 用户将悬停 View 拖到屏幕底部附近并将其
我正在为 iOS 制作一款游戏,您主要是在屏幕上拖动大型对象。当我每 5 分钟左右在实际的 iPad/iPhone 上运行游戏一段时间(在屏幕上连续拖动对象)时,拖动的对象会卡顿大约 10-30 秒。
假设我有两根手指触摸 iPhone 的屏幕,但只有一根手指在移动。 TouchesMoved 只会显示一根手指(事件)。 我如何知道 TouchesMoved 也指的是两个手指中的哪一个? 最佳答案
我正在使用这种方法在 View 中移动我的对象: - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 在这个方法中,我接下
我正在使用带有坐标系的 touchesMoved 来检测和响应屏幕特定区域内的用户触摸。例如,如果我有一个虚拟键盘并且用户在键上滑动,它会读取坐标并响应: - (void)touchesMoved:(
我正在尝试创建一个简单的应用程序,您可以通过触摸 UIImageView 并拖动他来移动 UIImageView。 我的 UIImageView 叫做 imv -(void ) touchesMove
我有一个 pie chart这是使用 UIBezierPath 制作的。我现在需要那些单独的路径(饼图)是可扩展的。我相信您需要一个 View 才能使用收缩缩放,所以我认为 touchesMoved:
我正在尝试为“addCurveToPoint:”或“addQuadCurveToPoint:”计算控制点,但我不知道该怎么做。 我尝试了一些代码示例,但没有...有人可以帮我吗? 谢谢! 最佳答案 检
我有一个 spritenode 和一个十字线节点,我希望当玩家触摸 spritenode 并移动十字线时,十字线也会移动。 override func touchesMoved(touches:
背景 我想在 View 从左向右拖动时为其设置动画,反之亦然。为了确定它被拖向哪个方向,我跟踪它所在的最后一个 x 位置,然后将其与当前位置进行比较。 代码 var lastX: CGFloat =
我在移动一些节点时遇到了问题,但我(终于)找到了一种方法,但后来我遇到了一个问题:当两个节点接触时,触摸将从一个节点移动到另一个节点!但我想要的是当我触摸一个节点时我会移动它直到我的手指离开屏幕这是我
我想用手指在屏幕上画一个箭头。我的想法是通过触摸屏幕来设置箭头的初始坐标,当我在屏幕上拖动时,箭头会延伸并跟随我的手指。箭头的高度和宽度将相同,重要的是箭头的大小。当我将它拖离起点时,箭头会变长。我用
我目前正在使用 touchesMoved 允许用户向上滑动,向上滑动的次数越多,增加的次数就越大。我不认为我可以在这里使用 UIGestureRecognizer,因为它不会记录每一个 Action
我的图片似乎在我每次更新时触摸的位置下方渲染。另一次它按预期进行。 我希望图像从上次移动时开始移动偏移量,而不会闪烁回到触摸位置。我该怎么做? 代码如下: int xposStamp; int ypo
我是一名优秀的程序员,十分优秀!