Monday, 17 March 2014

Uiscrollview centre image weird code


ive found code on ray wenderlich.
code:
uiimage *image = [uiimage imagenamed:@"photo1.png"];  self.imageview = [[uiimageview alloc] initwithimage:image];  self.imageview.frame = (cgrect){.origin=cgpointmake(0.0f, 0.0f), .size=image.size};
what's going on line of code?
code:
self.imageview.frame = (cgrect){.origin=cgpointmake(0.0f, 0.0f), .size=image.size}
.origin property assume? property of what? , it's written in curly brackets, block, seems casted cgrect. i'm confused, has blown me out of water.

code assigning self.imageview.frame as?

thanks

source: http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
 

first, cgrect typedef name struct type. cgrect not class, .frame member not property.

{ } notation initialized-struct notation unnamed struct. unnamed struct assigned .frame member. since .frame member struct, { } notation permitted in struct assignments.

{.origin=something, .size=otherthing} designated initializer. is, within { } initializer designate struct member names initialized. classical struct initializer requires in-order listing of initializer values; can't provide member names, have know exact order of members.

whole thing equivalent to:
code:
cgrect r;  // struct definition (r cgrect struct)  r.origin = cgpointmake(0.0f, 0.0f);  // struct assignment of cgpoint r.origin  r.size = image.size;  // struct assignment of image.size r.size  self.imageview.frame = r;  // struct assignment of r imageview.frame  
in last line, imageview property of self, .frame struct member of object.



designated initializers added in c99. see wikipedia article:
http://en.wikipedia.org/wiki/struct_(c_programming_language)#struct_initialization

try google searches c designated initializer.
 


Forums iPhone, iPad, and iPod Touch iOS Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

No comments:

Post a Comment