自訂View,android自訂view

來源:互聯網
上載者:User

自訂View,android自訂view

Ios裡的自訂View和Android類似

在.h檔案設定他的一些屬性,方法等

在.m檔案裡實現

.h檔案

#import <UIKit/UIKit.h>


CGPoint CGRectGetCenter(CGRect rect);

CGRect  CGRectMoveToCenter(CGRect rect,CGPoint center);


@interface UIView (ViewFrameGeometry)

@propertyCGPoint origin;

@property CGSize size;


@property (readonly)CGPoint bottomLeft;

@property (readonly)CGPoint bottomRight;

@property (readonly)CGPoint topRight;


@propertyCGFloat height;

@property CGFloat width;


@property CGFloat top;

@property CGFloat left;


@propertyCGFloat bottom;

@property CGFloat right;


- (void) moveBy: (CGPoint) delta;

- (void) scaleBy: (CGFloat) scaleFactor;

- (void) fitInSize: (CGSize) aSize;

@end



.m檔案



#import "UIViewExt.h"


CGPoint CGRectGetCenter(CGRect rect)

{

   CGPoint pt;

    pt.x =CGRectGetMidX(rect);

    pt.y =CGRectGetMidY(rect);

   return pt;

}


CGRect CGRectMoveToCenter(CGRect rect,CGPoint center)

{

   CGRect newrect = CGRectZero;

    newrect.origin.x = center.x-CGRectGetMidX(rect);

    newrect.origin.y = center.y-CGRectGetMidY(rect);

    newrect.size = rect.size;

   return newrect;

}


@implementation UIView (ViewGeometry)


// Retrieve and set the origin

- (CGPoint) origin

{

returnself.frame.origin;

}


- (void) setOrigin: (CGPoint) aPoint

{

CGRect newframe =self.frame;

newframe.origin = aPoint;

self.frame = newframe;

}



// Retrieve and set the size

- (CGSize) size

{

returnself.frame.size;

}


- (void) setSize: (CGSize) aSize

{

CGRect newframe =self.frame;

newframe.size = aSize;

self.frame = newframe;

}


// Query other frame locations

- (CGPoint) bottomRight

{

CGFloat x =self.frame.origin.x +self.frame.size.width;

CGFloat y =self.frame.origin.y +self.frame.size.height;

returnCGPointMake(x, y);

}


- (CGPoint) bottomLeft

{

CGFloat x =self.frame.origin.x;

CGFloat y =self.frame.origin.y +self.frame.size.height;

returnCGPointMake(x, y);

}


- (CGPoint) topRight

{

CGFloat x =self.frame.origin.x +self.frame.size.width;

CGFloat y =self.frame.origin.y;

returnCGPointMake(x, y);

}



// Retrieve and set height, width, top, bottom, left, right

- (CGFloat) height

{

returnself.frame.size.height;

}


- (void) setHeight: (CGFloat) newheight

{

CGRect newframe =self.frame;

newframe.size.height = newheight;

self.frame = newframe;

}


- (CGFloat) width

{

returnself.frame.size.width;

}


- (void) setWidth: (CGFloat) newwidth

{

CGRect newframe =self.frame;

newframe.size.width = newwidth;

self.frame = newframe;

}


- (CGFloat) top

{

returnself.frame.origin.y;

}


- (void) setTop: (CGFloat) newtop

{

CGRect newframe =self.frame;

newframe.origin.y = newtop;

self.frame = newframe;

}


- (CGFloat) left

{

returnself.frame.origin.x;

}


- (void) setLeft: (CGFloat) newleft

{

CGRect newframe =self.frame;

newframe.origin.x = newleft;

self.frame = newframe;

}


- (CGFloat) bottom

{

returnself.frame.origin.y + self.frame.size.height;

}


- (void) setBottom: (CGFloat) newbottom

{

CGRect newframe =self.frame;

newframe.origin.y = newbottom -self.frame.size.height;

self.frame = newframe;

}


- (CGFloat) right

{

returnself.frame.origin.x + self.frame.size.width;

}


- (void) setRight: (CGFloat) newright

{

CGFloat delta = newright - (self.frame.origin.x +self.frame.size.width);

CGRect newframe =self.frame;

newframe.origin.x += delta ;

self.frame = newframe;

}


// Move via offset

- (void) moveBy: (CGPoint) delta

{

CGPoint newcenter =self.center;

newcenter.x += delta.x;

newcenter.y += delta.y;

self.center = newcenter;

}


// Scaling

- (void) scaleBy: (CGFloat) scaleFactor

{

CGRect newframe =self.frame;

newframe.size.width *= scaleFactor;

newframe.size.height *= scaleFactor;

self.frame = newframe;

}


// Ensure that both dimensions fit within the given size by scaling down

- (void) fitInSize: (CGSize) aSize

{

CGFloat scale;

CGRect newframe =self.frame;

if (newframe.size.height && (newframe.size.height > aSize.height))

{

scale = aSize.height / newframe.size.height;

newframe.size.width *= scale;

newframe.size.height *= scale;

}

if (newframe.size.width && (newframe.size.width >= aSize.width))

{

scale = aSize.width / newframe.size.width;

newframe.size.width *= scale;

newframe.size.height *= scale;

}

self.frame = newframe;

}

@end


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.