0

我有 8 个 UIButton。现在我想在这些按钮上创建线条。

我试过了,我能画一条线。请检查我在做什么。

我的绘图视图.h

#import <UIKit/UIKit.h>

@interface MyDrawingView : UIView{

CGPoint fromPoint;
CGPoint toPoint;
UIColor* currentColor;



UIImage *backgroundImage;
}

@property CGPoint fromPoint;
@property CGPoint toPoint;
@property(nonatomic,retain) UIColor* currentColor;

@end



MyDrawingView.m

#import "MyDrawingView.h"

@implementation MyDrawingView

@synthesize fromPoint;
@synthesize toPoint;
@synthesize currentColor;



- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code

    backgroundImage = [UIImage imageNamed:@"office.jpeg"];
}
return self;
}


- (void)drawRect:(CGRect)rect {

//CGPoint drawingTargetPoint = CGPointMake(100,0);
//[backgroundImage drawAtPoint:drawingTargetPoint];

CGContextRef    context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,10);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextMoveToPoint(context,fromPoint.x , fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);


}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touchPoint = [touches anyObject];
fromPoint = [touchPoint locationInView:self];
toPoint = [touchPoint locationInView:self];

[self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
toPoint=[touch locationInView:self];
[self setNeedsDisplay];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
toPoint = [touch locationInView:self];
[self setNeedsDisplay];
}

现在在另一堂课上,我正在使用 8 个按钮viewDidLoad,请检查我在做什么。

-(void)viewDidLoad{

8 UIButtons (8 buttons in one line and 10 pixels gap in X-position after every button)

[self method_Call_drawView];
}




-(void)method_Call_drawView{
v = [[MyDrawingView alloc]initWithFrame:CGRectMake(100,350,400,400)]; 
v.backgroundColor = [UIColor whiteColor];
[v setNeedsDisplay];
[self.view addSubview:v];
}

 .h file
#import <UIKit/UIKit.h>


@class MyDrawingView;
@interface iPad_Level1 : UIViewController{

UIButton *btn1;
UIButton *btn2;
UIButton *btn3;
UIButton *btn4;
UIButton *btn5;
UIButton *btn6;
UIButton *btn7;
UIButton *btn8;
MyDrawingView *v;
}
@property(nonatomic,retain)MyDrawingView *v;
@end

我能够创建视图并在视图上画一条线,但我无法触摸UIButtons并在其上创建一条线。

无论我点击哪里,我的线都会增加和减少,但我希望这条线在我的UIButton.

请检查我要做什么 itunes.apple.com/in/app/word-search-puzzles/id609067187?mt=8,检查按钮上的黄线和蓝线。

任何想法或建议都将受到高度欢迎。

4

2 回答 2

2

你可以在你想要的按钮之前添加一个 1px 高度的子视图。

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 1)]; // change the 200 to fit your screen
lineView.backgroundColor = [UIColor blackColor];
[self.view addSubview:lineView];

当用户单击按钮时移动这条线,请使用:

[UIView animateWithDuration:0.5
                              delay:0.5
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {                 
             CGRect frame = lineView .frame;
             frame.origin.y = 100;   // for move
             frame.size.width = 50; // for re-size
             lineView .frame = frame;
         }
             completion:^(BOOL finished)
         {
             NSLog(@"Completed");
         }
];
于 2013-09-10T07:40:08.273 回答
0
#import <QuartzCore/CAGradientLayer.h>

导入这个并在项目中添加quartzcore框架

-(IBAction)btnTapped:(id)sender{
    UIButton *m_btnTemp=((UIButton *)sender);
    m_btnTemp.layer.borderColor=[UIColor blackColor];
    m_btnTemp.layer.borderWidth= 1.0f;
}

当您要删除边框颜色集时borderWidth=0;

于 2013-09-10T07:57:50.610 回答