0

我想以编程方式将 UIButton 完全放在贝塞尔路径上,但每次我更改路径坐标时,按钮位置都会完全改变。以下是我的代码。请帮助我将 UIbutton 准确定位在 Bezier 路径上。顺便说一句,我将 Button 放在直线路径上没有任何问题,但是当它出现对角线时,一切都搞砸了

//
//  HomeViewController.m
//  POCInitial
//
//  Created by Hammy Usmani on 25/06/2013.
//  Copyright (c) 2013 Hammy Usmani. All rights reserved.
//

#import "HomeViewController.h"
#import "RectangleView.h"

@interface HomeViewController ()

@end

@implementation HomeViewController
@synthesize drawpad;
@synthesize firstPoint, secondPoint, thirdPoint, fourthPoint;
@synthesize path;
@synthesize strokeValue;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.strokeValue = 15.0;
    [self setPathPoint];
    [self setPathForPoints];
    [self clickLineOne];
    [self clickLineTwo];
    [self clickLineThree];
    [self clickLineFour];
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma Button Function
-(void)clickLineOne
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self
               action:@selector(calculateDistance:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"" forState:UIControlStateNormal];
    button.frame = CGRectMake([firstPoint.x floatValue], [firstPoint.y floatValue] - 41, [secondPoint.x floatValue] - [firstPoint.x floatValue], self.strokeValue);
    button.tag = 1;
    [button setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:button];
}
-(void)clickLineTwo
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self
               action:@selector(calculateDistance:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"" forState:UIControlStateNormal];
    button.frame = CGRectMake([secondPoint.x floatValue], [secondPoint.y floatValue] - 41, self.strokeValue, [thirdPoint.y floatValue] - [secondPoint.y floatValue]);
    button.tag = 2;
    [button setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:button];
}
-(void)clickLineThree
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self
               action:@selector(calculateDistance:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"" forState:UIControlStateNormal];
    double aSquare = pow(abs([fourthPoint.y floatValue] - [thirdPoint.y floatValue]), 2.0);
    double bSquare = pow(abs([fourthPoint.x floatValue] - [thirdPoint.x floatValue]), 2.0);
    double length = sqrt(aSquare + bSquare);

    button.tag = 3;
    [button setBackgroundColor:[UIColor yellowColor]];
    if([thirdPoint.y floatValue] != [fourthPoint.y floatValue])
    {
        CGFloat angle = [self pointPairToBearingDegrees:thirdPoint secondPoint:fourthPoint];
        CGAffineTransform rotationTransform = CGAffineTransformIdentity;
        rotationTransform = CGAffineTransformMakeRotation(angle);
        button.transform = rotationTransform;
    }
    button.frame = CGRectMake([thirdPoint.x floatValue], [thirdPoint.y floatValue]-41, -length, self.strokeValue);
    [self.view addSubview:button];
}
-(void)clickLineFour
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self
               action:@selector(calculateDistance:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"" forState:UIControlStateNormal];
    button.frame = CGRectMake([fourthPoint.x floatValue]-5, [fourthPoint.y floatValue]-82, self.strokeValue, -([fourthPoint.y floatValue] - [firstPoint.y floatValue])+41);
    button.tag = 4;
    [button setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:button];
}
-(void)calculateDistance:(id)sender
{
    UIButton *button = (UIButton *)sender;
    if(button.tag == 1)
    {
        int distance = abs([secondPoint.x floatValue] - [firstPoint.x floatValue]) +abs([secondPoint.y floatValue] - [firstPoint.y floatValue]);
        UILabel *lblDistance = [[UILabel alloc] initWithFrame:CGRectMake(([secondPoint.x floatValue] + [firstPoint.x floatValue])/2, [firstPoint.y floatValue] - 41, [secondPoint.x floatValue] - [firstPoint.x floatValue], self.strokeValue)];
        lblDistance.text = [NSString stringWithFormat:@"%d", distance];
        lblDistance.backgroundColor = [UIColor clearColor];
        lblDistance.textColor = [UIColor whiteColor];
        [self.view addSubview:lblDistance];
    }
    else if(button.tag == 2)
    {
        int distance = abs([thirdPoint.x floatValue] - [secondPoint.x floatValue]) +abs([thirdPoint.y floatValue] - [secondPoint.y floatValue]);
        UILabel *lblDistance = [[UILabel alloc] initWithFrame:CGRectMake([secondPoint.x floatValue]-20, [secondPoint.y floatValue] - 41, self.strokeValue+15,  [thirdPoint.y floatValue] - [secondPoint.y floatValue])];
        lblDistance.text = [NSString stringWithFormat:@"%d", distance];
        lblDistance.backgroundColor = [UIColor clearColor];
        lblDistance.textColor = [UIColor whiteColor];
        [self.view addSubview:lblDistance];
    }
    else if(button.tag == 3)
    {
        double aSquare = pow(abs([fourthPoint.y floatValue] - [thirdPoint.y floatValue]), 2.0);
        double bSquare = pow(abs([fourthPoint.x floatValue] - [thirdPoint.x floatValue]), 2.0);
        double length = sqrt(aSquare + bSquare);

        int distance = abs([fourthPoint.x floatValue] - [thirdPoint.x floatValue]) +abs([fourthPoint.y floatValue] - [thirdPoint.y floatValue]);
        UILabel *lblDistance = [[UILabel alloc] initWithFrame:CGRectMake(([thirdPoint.x floatValue] + [fourthPoint.x floatValue])/2, (([thirdPoint.y floatValue] + [fourthPoint.y floatValue])/2)-90, length, self.strokeValue)];
        lblDistance.text = [NSString stringWithFormat:@"%d", distance];
        lblDistance.backgroundColor = [UIColor clearColor];
        lblDistance.textColor = [UIColor whiteColor];
        [self.view addSubview:lblDistance];
    }
    else
    {
        int distance = abs([firstPoint.x floatValue] - [fourthPoint.x floatValue]) +abs([firstPoint.y floatValue] - [fourthPoint.y floatValue]);
        UILabel *lblDistance = [[UILabel alloc] initWithFrame:CGRectMake([fourthPoint.x floatValue]-5, [fourthPoint.y floatValue]-82, self.strokeValue+15, -([fourthPoint.y floatValue] - [firstPoint.y floatValue])+41)];
        lblDistance.text = [NSString stringWithFormat:@"%d", distance];
        lblDistance.backgroundColor = [UIColor clearColor];
        lblDistance.textColor = [UIColor whiteColor];
        [self.view addSubview:lblDistance];

    }
}
#pragma Coordinates Functions
- (CGFloat) pointPairToBearingDegrees:(PointScreen*)startingPoint secondPoint:(PointScreen*) endingPoint
{
    CGPoint originPoint = CGPointMake([endingPoint.x floatValue] - [startingPoint.x floatValue], [endingPoint.y floatValue] - [startingPoint.y floatValue]); // get origin point to origin by subtracting end from start
    float bearingRadians = atan2(originPoint.y, originPoint.x); // get bearing in radians
    float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
    bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity
    return bearingDegrees;
}
-(void)setPathPoint
{   firstPoint = [[PointScreen alloc] initWithxCoordinate:50.0 andWithyCoordinate:50.0];
    secondPoint = [[PointScreen alloc] initWithxCoordinate:200.0 andWithyCoordinate:50.0];
    thirdPoint = [[PointScreen alloc] initWithxCoordinate:200.0 andWithyCoordinate:200.0];
    fourthPoint = [[PointScreen alloc] initWithxCoordinate:50.0 andWithyCoordinate:150.0];
}

-(void)setPathForPoints
{
    self.path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointMake([firstPoint.x floatValue], [firstPoint.y floatValue])];
    [path addLineToPoint:CGPointMake([secondPoint.x floatValue], [secondPoint.y floatValue])];
    [path addLineToPoint:CGPointMake([thirdPoint.x floatValue], [thirdPoint.y floatValue])];
    [path addLineToPoint:CGPointMake([fourthPoint.x floatValue], [fourthPoint.y floatValue])];
    [path closePath];
    UIGraphicsBeginImageContext(self.view.frame.size);
    path.lineCapStyle = kCGLineCapRound;
    path.lineWidth = 15.0f;
    [[UIColor blackColor] setStroke];
    [[UIColor redColor] setFill];
    [path stroke];
    [path fill];
    self.drawpad.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}
@end

提前致谢

4

0 回答 0