0

我正在尝试在自定义视图(rectView)中创建一个 CGRect,它会随着您移动滑块而上下移动。

我的滑块的 IBAction 调用以下方法:(这很好)

- (void)moveRectUpOrDown:(int)y
{
    self.verticalPositionOfRect += y;
    [self setNeedsDisplay];
}

我的 drawRect 方法:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat size = 100;
    self.rect = CGRectMake((self.bounds.size.width / 2) - (size / 2), 
                                 self.verticalPositionOfRect - (size / 2), 
                                 size, 
                                 size);
    CGContextAddRect(context, self.rect);
    CGContextFillPath(context);
}

我的自定义视图的 initWithFrame 使用 setNeedsDisplay 调用 drawRect 方法,但由于某种原因 moveRectUpOrDown 不会调用 drawRect。

任何想法我做错了什么?

为清楚起见,整个实现如下:

//ViewController.h
#import <UIKit/UIKit.h>
#import "rectView.h"

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet rectView *rectView;
- (IBAction)sliderChanged:(id)sender;
@end

//ViewController.m
#import "ViewController.h"

@implementation ViewController
@synthesize rectView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.rectView = [[rectView alloc] initWithFrame:self.rectView.frame];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)sliderChanged:(id)sender 
{
    UISlider *slider = sender;
    CGFloat sliderValue = slider.value;
    [self.rectView moveRectUpOrDown:sliderValue];
}
@end

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

@interface rectView : UIView
- (void)moveRectUpOrDown:(int)y;
@end

//rectView.m
#import "rectView.h"

@interface rectView ()
@property CGRect rect;
@property int verticalPositionOfRect;
@end

@implementation rectView
@synthesize rect, verticalPositionOfRect;

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

        self.verticalPositionOfRect = (self.bounds.size.height / 2);
        [self setNeedsDisplay];
    }
    return self;
}

- (void)moveRectUpOrDown:(int)y
{
    self.verticalPositionOfRect += y;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat size = 100.0;
    self.rect = CGRectMake((self.bounds.size.width / 2) - (size / 2), 
                                 self.verticalPositionOfRect - (size / 2), 
                                 size, 
                                 size);
    CGContextAddRect(context, self.rect);
    CGContextFillPath(context);
}

@end

谢谢您的帮助 :)

4

3 回答 3

2

好的,所以答案是视图实际上从未添加到viewController的视图中。

你有一个- 所以我假设你将一个IBOutlet组件拖到Interface Builder 中,然后将它的类更改为,这一切都很好,但是用一个新对象擦除这个对象rectViewUIViewviewrectViewviewDidLoad

self.rectView = [[rectView alloc] initWithFrame:self.rectView.frame];

所以这现在不再是从 xib 加载的对象。

随后,该视图从未真正添加到视图层次结构中,因此它永远不会自行绘制,因为它没有意义。

所以解决方案是删除我上面突出显示的行。


笔记

对于这样的实现,我可能仍然会选择我的其他答案,但是绊倒并学习新事物会很有帮助。

rectView不遵循命名约定。理想情况下,您应该以 2-3 个字母前缀(可能是您的首字母或公司名称)开始您的班级名称,然后是驼峰式命名。

于 2012-04-07T03:16:26.400 回答
1

我看不到您如何初始化 CGFloat 大小。你能在里面放一个值,看看它是如何工作的吗?其余的代码对我来说看起来没问题。

于 2012-04-07T02:06:04.507 回答
1

更简单的事情是添加一个UIView并对其进行动画处理。

为此添加一个 ivarUIView

// .h
@property (nonatomic, strong) UIView *animatedView;

// .m
@synthesize animatedView = _animatedView;

然后用这个替换你的逻辑

- (void)moveRectUpOrDown:(int)y;
{
    CGRect frame = self.animatedView.frame;
    frame.origin.y += y;

    // If you want the change to animate uncomment this
    // [UIView animateWithDuration:0.25f
    //                  animations:^{
    //                      self.animatedView.frame = frame;
    //                  }];

    // If you don't want the change to animate uncomment this
    // self.animatedView.frame = frame;
}
于 2012-04-07T02:49:15.213 回答