gpt4 book ai didi

objective-c - Objective-C : fix distance between images in touches moved

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

当执行触摸移动时,如何设置我的图像(点)与新图像(点)的固定距离相同?

enter image description here

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];

UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Crayon_Black.png"]];
imageView.center = touchLocation;

[drawImage addSubview:imageView];
}

我希望这是有道理的。我只需要完成我的学校项目。提前谢谢大家。

最佳答案

只要您的手指移动速度不太快,此解决方案就有效:

@interface ViewController : UIViewController {
CGPoint lastLocation_;
CGFloat accumulatedDistance_;
}

...

-(CGFloat) distanceFromPoint:(CGPoint)p1 ToPoint:(CGPoint)p2 {
CGFloat xDist = (p2.x - p1.x);
CGFloat yDist = (p2.y - p1.y);
return sqrt((xDist * xDist) + (yDist * yDist));
}

-(void) addImageAtLocation:(CGPoint)location {
UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Crayon_Black.png"]];
imageView.center = location;
[self.view addSubview:imageView];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
lastLocation_ = [[touches anyObject] locationInView:self.view];
accumulatedDistance_ = 0;
[self addImageAtLocation:lastLocation_];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];

CGFloat distance = [self distanceFromPoint:touchLocation ToPoint:lastLocation_];
accumulatedDistance_ += distance;
CGFloat fixedDistance = 40;
if (accumulatedDistance_ > fixedDistance) {
[self addImageAtLocation:touchLocation];
while (accumulatedDistance_ > fixedDistance) {
accumulatedDistance_ -= fixedDistance;
}
}

lastLocation_ = touchLocation;
}

关于objective-c - Objective-C : fix distance between images in touches moved,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11669386/

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