ios – Ipad的CGRect中心

我正在使用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,currentPoint;
@property (nonatomic,assign) CGRect imageFrame;
@property (nonatomic,assign) BOOL fingerMoved;
@property (nonatomic,assign) float navbarHeight;


@property (strong,nonatomic) NSManagedobjectContext *managedobjectContext;

解决方法

假设图像是UIImage类型,那么:
CGRect imageFrame = CGRectMake(
    CGRectGetMidX(self.view.frame) - (image.size.width / 2.0),CGRectGetMidY(self.view.frame) - (image.size.height / 2.0),image.size.height);

假设imageView的类型为UIImageView,则:

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...