Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CACornerMask with Objective-C

With iOS 11 we now have CACornerMask and the maskedCorners property on CALayers. However I am unable to set multiple corners to be rounded at the same time in Objective-C.

The swift implementation is:

view.layer.cornerRadius = 10
view.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]

But in Objective-C this does not work:

view.layer.maskedCorners = @[kCALayerMinXMinYCorner, kCALayerMaxXMinYCorner];

I think this is because Swift is using an OptionSetType but I don't know what the Objective-C equivalent to that is.

Has anyone been able to successfully use multiple masked corners with Objective-C? Thanks in advance for your help.

like image 263
Seslyn Avatar asked Nov 14 '17 14:11

Seslyn


People also ask

Is it possible to use Objective-C with C++?

Typically, your C++ code will define some class you'd like to use. You could switch your whole project to Objective-C++ by renaming all the .m files to .mm, and freely mix C++ and Objective-C.

How to create an Objective-C project?

Name your project and select the language as Objective C. Select the desired location and save your project. Now, in the Project Navigator, you will see a 'main' file. This file is similar to the main function in C programming. The code execution starts from here. To add new files, right-click on your Project and select New File.

Why can't I use cppobject in Objective-C?

The compiler will get enormously confused by class CppObject and the block following it, as that's simply not valid Objective-C syntax. The error will typically be something like.

How to fix viewcontroller cannot mix Objective-C with C++ in Xcode?

To fix this issue, rename ViewController.m to ViewController.mm. This simple naming convention tells Xcode that ViewController wants to mix Objective-C with C++. After renaming the file, the error should disappear. Let’s make the app more interactive by adding a button.


1 Answers

In Objective-C you need to use bitwise OR (|) to combine the values:

view.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
like image 51
rmaddy Avatar answered Oct 21 '22 23:10

rmaddy