【问题标题】:CGRect Center for IpadIpad CGRect 中心
【发布时间】:2014-08-06 09:53:34
【问题描述】:

我正在使用 CGRect 创建一个图像框架。我想将创建的矩形居中。

我在此处以及 Apple 文档中查看了执行此操作的最佳方法,并找到了获取中心坐标的 CGRectGetMidY 和 CGRectGetMidX。

当我尝试在自己的代码中实现这一点时,我遇到了问题。我在 UIIMageView 类型的对象上找不到属性大小错误

       #import "MyViewController.h"



    @interface MyViewController ()

    @end

    @implementation MyViewController

    @synthesize mySignatureImage;
    @synthesize lastContactPoint1, lastContactPoint2, currentPoint;
    @synthesize imageFrame;
    @synthesize fingerMoved;
    @synthesize navbarHeight;

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];



        self.view.backgroundColor = [UIColor lightGrayColor];

        CGRect mySignatureImageFrame = CGRectMake(
                                       CGRectGetMidX(self.view.frame) - (mySignatureImage.size.width/ 2.0),
                                       CGRectGetMidY(self.view.frame) - (mySignatureImage.size.height / 2.0),
                                       image.size.width,
                                       image.size.height);




#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController <UIAlertViewDelegate>

@property (nonatomic, strong) UIImageView *mySignatureImage;
@property (nonatomic, assign) CGPoint lastContactPoint1, lastContactPoint2, currentPoint;
@property (nonatomic, assign) CGRect imageFrame;
@property (nonatomic, assign) BOOL fingerMoved;
@property (nonatomic, assign) float navbarHeight;


@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

【问题讨论】:

  • 您希望图像帧有多大?
  • imageView.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));
  • 它的意思是足够大,用户可以写签名,非常大。我玩过数字和大小,但居中是导致问题的原因。 @Droppy
  • @KudoCC 感谢您的回复,试过了,我得到一个未知类型名称“imageView”你的意思是 UIIMageView 错误
  • @aramirez imageView 是UIImageView

标签: ios objective-c uiimage cgrect


【解决方案1】:

假设imageUIImage 类型,那么:

CGRect imageFrame = CGRectMake(
    CGRectGetMidX(self.view.frame) - (image.size.width / 2.0),
    CGRectGetMidY(self.view.frame) - (image.size.height / 2.0),
    image.size.width,
    image.size.height);

假设imageViewUIImageView 类型,那么:

CGRect imageFrame = CGRectMake(
    CGRectGetMidX(self.view.frame) - CGRectGetMidX(imageView.frame),
    CGRectGetMidY(self.view.frame) - CGRectGetMidY(imageView.frame),
    CGRectGetWidth(imageView.frame),
    CGRectGetHeight(imageView.frame));

【讨论】:

  • 感谢您的评论,当我尝试这个时,我得到一个“在'NSData'类型的对象上找不到“属性'大小'
  • @aramirez 我的回答假设image 是一个UIImage 对象。您没有在问题中显示图片是什么,因此您必须扩展问题中的代码才能让我更完整地回答。
  • 我已经更新了我上面的代码sn-p供大家参考,谢谢
  • 感谢您的更新,所以当我进行此更改时,我收到一个语义错误,即在 struct cgrect 中没有名为 frame 的成员
  • 不再了,感谢它编译的更新,但是当我运行模拟器时,我根本看不到它正在制作的矩形
【解决方案2】:

您不需要计算中心点,因为它无论如何都可用于视图:

CGRect superviewBounds = superview.bounds;
imageView.center = CGPointMake(CGRectGetMidX(superviewBounds), CGRectGetMidY(superviewBounds));

【讨论】:

  • @holex 不是。 boundscenter 是实际用于设置视图的frame。因此,通过将视图的 center 属性设置为您希望它位于的任何点,可以轻松地将图像居中或放置在任何您想要的位置。
  • 如果您有两个视图:view1 和框架 (100.0, 100.0, 50.0, 30.0) 并且它有一个子视图为 view2 和框架 (0.0, 0.0, 50.0, 30.0)view2.center 永远不会等于 view2.superview.center。 .. 仅当超级视图精确覆盖整个屏幕时,您的想法才有效——在任何其他情况下,它都不会按您的意愿工作。
【解决方案3】:
- (void)viewDidAppear:(BOOL)animated {


img.center = CGPointMake(CGRectGetMidX([self.view bounds]), CGRectGetMidY([self.view   bounds]));
}

/*write code in viewDidApper */

【讨论】:

  • 欢迎来到 SO!请为您的代码添加一个简短的说明。
【解决方案4】:
CGPoint (^CGRectGetCenter)(CGRect) = ^(CGRect rect)
{
    return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
};

示例:Mirant 的上述回答

【讨论】:

    【解决方案5】:
    let center = CGPoint(x: rect.midX, y: rect.midY)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多