iOS Question how to use UIBezierPath

chjk

Member
Licensed User
Longtime User
This is Object C
B4X:
- (void)drawRect:(CGRect)rect 
{ 
    UIColor *color = [UIColor redColor]; 
    [color set];     
    UIBezierPath* aPath = [UIBezierPath bezierPath]; 
    aPath.lineWidth = 5.0;      
    aPath.lineCapStyle = kCGLineCapRound;
    aPath.lineJoinStyle = kCGLineCapRound;      
    // Set the starting point of the shape. 
    [aPath moveToPoint:CGPointMake(100.0, 0.0)]; 
    // Draw the lines 
    [aPath addLineToPoint:CGPointMake(200.0, 40.0)]; 
    [aPath addLineToPoint:CGPointMake(160, 140)]; 
    [aPath addLineToPoint:CGPointMake(40.0, 140)]; 
    [aPath addLineToPoint:CGPointMake(0.0, 40.0)]; 
    [aPath closePath];     
    [aPath stroke];//Draws line 
}
20130904194200078

This is my B4i
B4X:
#If OBJC
NSMutableArray *paths;
-(void)drawLineT: (UIView*)v
{
    CGRect bounds1 = v.bounds;
    [drawRect bounds1]
}
- (void)drawRect:(CGRect)rect
{
    UIColor *color = [UIColor redColor];
    [color set];    
    UIBezierPath* aPath = [UIBezierPath bezierPath];
    aPath.lineWidth = 5.0;   
    aPath.lineCapStyle = kCGLineCapRound;
    aPath.lineJoinStyle = kCGLineCapRound;    
    // Set the starting point of the shape.
    [aPath moveToPoint:CGPointMake(100.0, 0.0)];   
    // Draw the lines
    [aPath addLineToPoint:CGPointMake(200.0, 40.0)];
    [aPath addLineToPoint:CGPointMake(160, 140)];
    [aPath addLineToPoint:CGPointMake(40.0, 140)];
    [aPath addLineToPoint:CGPointMake(0.0, 40.0)];
    [aPath closePath];   
    [aPath stroke];//Draws line
}
#End If
I don't know b4i how to use UIView
 

chjk

Member
Licensed User
Longtime User
Another question from the Chinese Forum, notes are Chinese this
my b4i code
B4X:
#If OBJC
//Touch Event
-(void)tb: (float )X :(float )Y
{  
    CGPoint starPoint= CGPointMake(X, Y);
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path setLineWidth:5];
    [path setLineJoinStyle:kCGLineJoinRound];
    [path setLineCapStyle:kCGLineCapRound];
    [path moveToPoint:starPoint];
    [paths addObject:path];
}
//touch move
-(void)touchesMoved1:(float )X :(float )Y
{
    CGPoint movePoint=CGPointMake(X, Y);
    UIBezierPath *currentPath=[paths lastObject];
    [currentPath addLineToPoint:movePoint];
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    [self drawRect1:screenRect];
}
//touch up
//-(void)touchesEnded:(CGFloat *)X :(CGFloat *)Y
//{
//    [self touchesMoved:X:Y];
//}
//draw line
- (void)drawRect1:(CGRect)rect
{
    for (UIBezierPath *path in paths) {
        [path stroke];
   }
}
#End If
Sub Panel1_Touch(Action As Int, X As Float, Y As Float)
'    Dim pnl As Panel
'    pnl = Sender
  
    Select Action
    Case 0 'down
        Dim NativeMe As NativeObject = Panel1
        NativeMe.RunMethod("tb::",Array(x,y))
        isDown= True
    Case 1 'up
        isDown= False      
'        Dim NativeMe As NativeObject = Me
'        NativeMe.RunMethod("touchesEnded::",Array(x,y))
    Case 2 'move
        If isDown Then
            Dim NativeMe As NativeObject = Panel1
            NativeMe.RunMethod("touchesMoved1::",Array(x,y))
        End If
    End Select
End Sub
this is Chinese BBS OC
B4X:
//
// Drawing code
//
//  Created by apple on 14-6-12.
//  Copyright (c) 2014 itcase. All rights reserved.
//
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.获取手指对应的UItoch对象
         UITouch  *touch=[touches anyObject];
   
     //2.通过UIToch对象获取手指触摸的位置
     CGPoint starPoint=[touch locationInView:touch.view];   
     //3.当用户手指按下的时候创建一条路径
     UIBezierPath *path=[UIBezierPath bezierPath];   
     //设置路径的相关属性
     [path setLineWidth:5];
     [path setLineJoinStyle:kCGLineJoinRound];
     [path setLineCapStyle:kCGLineCapRound];
     //4.设置当前路径的起点
     [path moveToPoint:starPoint];   
    //5.将路径添加到数组中去
     [self.paths addObject:path];
}
//手指移动事件
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
      //1.获取手指对应的UIToch对象
     UITouch *touch=[touches anyObject];
     //2.通过UIToch对象获取手指触摸的位置
     CGPoint movePoint=[touch locationInView:touch.view];
     //3.取出当前的path
     UIBezierPath *currentPath=[self.paths lastObject];
     //4.设置当前路径的终点
     [currentPath addLineToPoint:movePoint];
     //5.调用drawRect方法重绘视图
     [self setNeedsDisplay];
}
////抬起手指
//-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    [self touchesMoved:touches withEvent:event];
//}
//画线
- (void)drawRect:(CGRect)rect
{
    //根据路径绘制所有的线段
     for (UIBezierPath *path in self.paths) {
         [path stroke];
     }
}
/**
  *  清空面板
  */
-(void)clearView
{
     //清空所有的路径
     [self.paths removeAllObjects];
     //调用方法重新绘图
     [self setNeedsDisplay];
}
/**
*  回退操作
  */
-(void)backView
{
     //移除路径数组中的最后一个元素(最后一条路径)
     [self.paths removeLastObject];
     //重新绘图
      [self setNeedsDisplay];
}
@end
 

Attachments

  • PanelTouchDraw.zip
    7.8 KB · Views: 206
Upvote 0
Top