首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGRect Ipad中心

CGRect Ipad中心
EN

Stack Overflow用户
提问于 2014-08-06 09:53:34
回答 5查看 11.8K关注 0票数 7

我正在使用CGRect创建一个图像帧。我想要对创建的矩形进行居中。

我在这里查看过,以及在苹果文档中找到了最好的方法,并找到了CGRectGetMidY和CGRectGetMidX,它们得到中心坐标。

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

代码语言:javascript
复制
       #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;
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-08-06 09:59:56

假设imageUIImage类型,那么:

代码语言:javascript
复制
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类型,那么:

代码语言:javascript
复制
CGRect imageFrame = CGRectMake(
    CGRectGetMidX(self.view.frame) - CGRectGetMidX(imageView.frame),
    CGRectGetMidY(self.view.frame) - CGRectGetMidY(imageView.frame),
    CGRectGetWidth(imageView.frame),
    CGRectGetHeight(imageView.frame));
票数 11
EN

Stack Overflow用户

发布于 2014-08-06 10:24:40

您不需要计算中心点,因为它对于视图是可用的:

代码语言:javascript
复制
CGRect superviewBounds = superview.bounds;
imageView.center = CGPointMake(CGRectGetMidX(superviewBounds), CGRectGetMidY(superviewBounds));
票数 7
EN

Stack Overflow用户

发布于 2014-08-06 10:31:29

代码语言:javascript
复制
- (void)viewDidAppear:(BOOL)animated {


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

/*write code in viewDidApper */
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25157265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档