如何向具有圆角的UIView添加阴影

时间:2019-01-20 21:06:56

标签: swift uiview shadow

我知道已经提出了这个问题,但是我尝试了必须回答的问题,但这似乎对我不起作用。

所以我想要一个UIView的侧面和按钮周围而不是顶部周围有阴影。我四舍五入怎么办?

UI如下所示:

enter image description here

这是我到目前为止尝试过的(似乎不起作用):

featureOneView.layer.cornerRadius = 10.0
        featureOneView.clipsToBounds = true


        featureOneView.layer.shadowOffset = CGSize(width: 0, height: 3)
        featureOneView.layer.shadowOpacity = 0.6
        featureOneView.layer.shadowRadius = 3.0
        featureOneView.layer.shadowColor = UIColor.gray.cgColor

2 个答案:

答案 0 :(得分:0)

我确实相信问题在于限制范围。如果要让视图限制边界,则应该有两个视图-一个用于阴影,一个用于内容。

这对您有用吗?

// corner radius
featureOneView.layer.cornerRadius = 10

// shadow
featureOneView.layer.shadowColor = UIColor.black.cgColor
featureOneView.layer.shadowOffset = CGSize(width: 3, height: 3)
featureOneView.layer.shadowOpacity = 0.6
featureOneView.layer.shadowRadius = 3.0

提供了快速的Google搜索:https://stackoverflow.com/a/34984063/6885097

答案 1 :(得分:0)

我编写了一个完美运行的IBDesingable类

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback, OnMapLongClickListener {

private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            centerMapOnLocation(lastKnownLocation, "Your location");
        }
    }

}
public void centerMapOnLocation(Location location, String title){
    LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());

    mMap.clear();

    if(title != "Your location") {
        mMap.addMarker(new MarkerOptions().position(userLocation).title(title));
    }
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 10));
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    mMap.setOnMapLongClickListener(this);

    Intent intent = getIntent();
    if(intent.getIntExtra("placeNumber", 0) == 0){
        //zoom in on user's location
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                centerMapOnLocation(location, "Your location");
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }};
        //If device is running SDK < 23
        if(Build.VERSION.SDK_INT < 23){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }
        else {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                centerMapOnLocation(lastKnownLocation, "Your location");
            }else{
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            }
        }
    }

}

@Override
public void onMapLongClick(LatLng latLng) {

    mMap.addMarker(new MarkerOptions().position(latLng).title("Your new memorable place"));

    Toast.makeText(this, "Location saved", Toast.LENGTH_LONG).show();
}

您可以添加框架/角半径/偏移/颜色/不透明 希望有帮助

相关问题