使用drawRect方法绘制的线不会滚动

时间:2013-11-21 05:01:44

标签: ios iphone uiscrollview drawrect

我正在创建一个应用程序,我想在scrollView上绘制线条。我能画线条。

这是我的代码

@interface GraphOnScrollView : UIScrollView  
@property(strong,nonatomic)NSMutableArray *intensityArray;
@end

import“GraphOnScrollView.h”

@implementation GraphOnScrollView

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

        self.userInteractionEnabled=YES;
        self.scrollEnabled=YES;
        UIButton *DirectMsgBtn1 =[UIButton buttonWithType:UIButtonTypeCustom];

        DirectMsgBtn1.titleLabel.font =[UIFont systemFontOfSize:12.0];
        [DirectMsgBtn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [DirectMsgBtn1 setTitle:@"direct message" forState:UIControlStateNormal];
        [DirectMsgBtn1 setBackgroundColor:[UIColor clearColor]];
        [DirectMsgBtn1 addTarget:self
                          action:@selector(DirectMessageViewPopUp:)
                forControlEvents:UIControlEventTouchDown];
        DirectMsgBtn1.frame = CGRectMake(0.0, 0, 100, 30);
        [self addSubview:DirectMsgBtn1];
        DirectMsgBtn1 = nil;

    }
    return self; }


// Only override drawRect: if you perform custom drawing. // An
empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code



    CGFloat y_Axix =20.0;
    CGFloat lineWidth=1.0;
    for (int i=0; i<[self.intensityArray count]; i++)
    {

        CGContextRef c = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(c, 2.0);
        lineWidth+=1;
        CGFloat red[4] = {1.0f, 0.5f, 0.0f, 1.0f};
        CGContextSetStrokeColor(c, red);
        CGContextBeginPath(c);

        CGFloat width=[[self.intensityArray objectAtIndex:i] floatValue];
        CGContextMoveToPoint(c, 5.0f, y_Axix);
        CGContextAddLineToPoint(c, width, y_Axix);
        CGContextStrokePath(c);

        CGContextAddArc(c,width,y_Axix,1.0f,0,2*3.1415926535898,1);
        CGContextDrawPath(c,kCGPathStroke);
        y_Axix=y_Axix+50;
    }

    NSLog(@"intensity array %@", self.intensityArray);



    self.contentSize = CGSizeMake(self.frame.size.width, 1000);

}

这是我用于在视图中添加scrollview的代码

GraphOnScrollView *GraphView =[[GraphOnScrollView
alloc]initWithFrame:CGRectMake(0.0, 150.0, 320.0, 280.0)];
    GraphView.backgroundColor =[UIColor whiteColor];
    GraphView.intensityArray =[NSMutableArray arrayWithObjects:@"60",@"100",@"40",@"10",@"20",@"40",@"40",@"100",
nil];


    [self.view addSubview:GraphView];

使用此代码,我绘制的线条不会滚动但滚动滚动视图。我不知道是什么问题。

由于

2 个答案:

答案 0 :(得分:1)

在绘制任何视图时,它会在该视图的canvas上绘制所有内容。 如果您正在使用UIScrollView,那么所有绘图都会在UIScrollView的画布上执行,因此它不可滚动。

要解决该问题,您可以在具有所需大小的单独UIView上创建,在其上执行任何绘图并在scrollview中添加该视图。我认为这将是最好的解决方案。

您还可以参考this链接获取更多帮助。

答案 1 :(得分:0)

替代方法(我实际上使用了uiview进行绘图并使其看起来像滚动)

保存触摸位置

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

}

记录移动的触摸点。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

}

现在您可以将点的坐标转换为

newX=x+(touchstart.x-touchMoved.x);//do this for x of every point
newy=y+(touchstart.y-touchMoved.y);// do this for y of every point

将上面的行放在一个函数中并从触摸调用它们移动你可以在触摸结束的方法中执行它,如果滞后可以用你。

点是平移点并计算用户触摸开始和移动或滑动的平移度