1 つの UIView クラスの drawRect メソッドで、矢印の長さを固定して尖った矢印を作成しています。ArrowView の中心と回転角度を計算し、矢印がソースから宛先を指しているようにビューに追加する必要があります。
私が使用しているコードは
-(void)addArrowForChessMove:(CGPoint)source andDestination:(CGPoint)destination
{
CGPoint centerOfArrow=CGPointMake((abs(source.x+destination.x)/2), (abs(source.y+destination.y)/2));
DrawExperiment *drw=[[DrawExperiment alloc]initWithArrowHeight:70];
drw.frame=CGRectMake(0,0, 25, 70);
[drw setCenter:centerOfArrow];
[drw setBackgroundColor:[UIColor clearColor]];
[drw setTag:1002];
float angleVal = atan2((destination.x - source.x) , (destination.y - source.y))*180/M_PI;
if ((destination.y -source.y) > 0)
{
if((destination.x - source.x)<0)
{
angleVal= angleVal+M_PI_2;
}
NSLog(@"%f====%f",(destination.y -source.y),(destination.x - source.x));
}
else if ((destination.y -source.y) < 0)
{
if((destination.x - source.x)<0)
{
angleVal=angleVal+M_PI_2;
}
else{
NSLog(@"%f====%f",(destination.y -source.y),(destination.x - source.x));
angleVal= angleVal+M_PI;
}
}
else if ((destination.x - source.x) < 0)
{
if((destination.y -source.y)>=0)
{
angleVal= angleVal+M_PI;
}
else
{
NSLog(@"%f====%f",(destination.y -source.y),(destination.x - source.x));
angleVal= M_PI;
}
}
drw.transform=CGAffineTransformMakeRotation(angleVal);
[self.view addSubview:drw];
[self.view bringSubviewToFront:[self.view viewWithTag:1002]];
}`
` コードは水平軸または垂直軸に対して正常に機能しますが、ポイントが 90 の倍数以外の角度にある場合、結果は時間的に逆になります。
私が見逃しているものは何ですか..ありがとう
矢印はどの方向にもできるので、調整コードを追加しました。これにより多くの問題が解決されましたが、45 度と 135 度にはまだ問題があります...
注:-マークダウンは健全な批判として歓迎しますが、改善できるように、コメントにマークダウンの理由を書いてください