简体   繁体   English

如何在ios中创建UIButtons的行?

[英]How to create line over UIButtons in ios?

I have 8 UIButtons. 我有8个UIButtons。 Now I want to create line overs these buttons. 现在我想创建这些按钮。

I tried and I am able to draw a line. 我试过了,我能划清界限。 Please check what I am doing. 请检查我在做什么。

MyDrawingView.h MyDrawingView.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];
}

Now in another class I am using 8 buttons on viewDidLoad , please check what I am doing. 现在在另一个班级我在viewDidLoad上使用8个按钮,请检查我在做什么。

-(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

I am able to create view and draw a line on a view but I am not able to touch UIButtons and create a line over it. 我能够在视图上创建视图并绘制一条线,但我无法触摸UIButtons并在其上创建一条线。

My line is increasing and decreasing wherever I click but I want this line on my UIButton . 无论我点击哪里,我的线都在增加和减少,但我想在我的UIButton上使用这一行。

please check what I am trying to do itunes.apple.com/in/app/word-search-puzzles/id609067187?mt=8, check yellow and blue line on the buttons. 请检查我想要做什么itunes.apple.com/in/app/word-search-puzzles/id609067187?mt=8,检查按钮上的黄色和蓝色线。

Any idea or suggestion would be highly welcome. 任何想法或建议都将受到高度欢迎。

You ca add a 1px height sub view before the button you want. 您可以在所需按钮之前添加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];

And for moving this line when user click a button use: 并且当用户单击按钮时移动此行使用:

[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");
         }
];
#import <QuartzCore/CAGradientLayer.h>

Import this and add quartzcore framework in the project 导入此项并在项目中添加quartzcore框架

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

When you want to remove the bordercolor set borderWidth=0; 当你想删除bordercolor时设置borderWidth=0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM